CMB2 / cmb2-attached-posts

Custom field for CMB2 for attaching posts to a page.
GNU General Public License v2.0
135 stars 62 forks source link

Problems with returned data. #31

Closed SvyrydovAndrey closed 7 years ago

SvyrydovAndrey commented 8 years ago

Hi guys, Thank you for your plugin. I have one small issue: When i tried get values from field I got string but not array.

$prefix = 'products';
$cmb = new_cmb2_box(array(
        'id' => $prefix.'related_list',
        'title' => 'Related products',
        'object_types' => array('product'), 
        'context' => 'normal',
        'priority' => 'high', 
        'show_names' => true,
    ));
$cmb->add_field( array(
        'name'     => __( 'Related Products', 'cmb2' ),
        'desc'     => __( 'Drag products from the left column to the right column to attach them to this page.<br />You may rearrange the order of the products in the right column by dragging and dropping.', 'cmb2' ),
        'id'       => $prefix.'related_list',
        'type'     => 'custom_attached_posts',
        'multiple' => true,
        'options'  => array(
            'show_thumbnails' => true,
            'filter_boxes'    => true,
            'query_args'      => array(
                'post_type' => 'product',
            ),
        )
    ) );
$products_list = get_post_meta(get_the_ID(), 'products_list', true);
print_r(gettype($products_list)); // result string
modemlooper commented 8 years ago

if third arg for get_post_meta is true it will be string.

https://developer.wordpress.org/reference/functions/get_post_meta/#return

vitaly-safonov commented 8 years ago

@SvyrydovAndrey $products_list = get_post_meta(get_the_ID(), 'products_list', false); Try this.