WordPress – Repeatable custom field values are outputted as a string and not an array

I have a repeatable text field “show_on_pages”, I have created a new post of this content type and added 3 values for “show_on_pages”. When i run the code below

$args = array('post_type' => 'my_post_type');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
    $meta = get_post_meta(get_the_id(),'show_on_pages');
    print_r($meta);
endwhile;

It returns a string and not an array

Read More

Array ( [0] => [“page1″,”page2″,”page3”] )

If I do

echo $meta[0];

The page will print “[“page1″,”page2″,”page3″]”

https://wordpress.org/plugins/custom-content-type-manager/

Related posts

1 comment

  1. You meta is stored as JSON.
    Do the following $pages=json_decode($meta[0]); and $pages will be an array which will look like this (from your example):

    Array
    (
        [0] => page1
        [1] => page2
        [2] => page3
    )
    

    Refer to this documentation for more info on json_decode function.

Comments are closed.