How do I use foreach to get content from a custom-field in multiple posts?
so I am trying to use a foreach to pull content from a custom field in multiple posts, but it just wont work. I can get it to work for a simple textarea string but I am struggling getting it to work for arrays. This is the working code for a string:
$posts = get_posts(array(
'numberposts' = -1,
'post_type' = 'post',));
foreach($posts as $post)
{
$string = get_post_meta($post-ID, 'simple_string', true);
echo $string;
}
And elsewhere in the site this code works for getting into a specific posts custom field array:
$arr = get_field('array');
$arr2 = $arr[0]['string'];
$string = implode(", ",$arr2);
echo $string;
Why doesn't this work?
$posts = get_posts(array(
'numberposts' = -1,
'post_type' = 'post',));
foreach($posts as $post)
{
$arr = get_post_meta($post-ID, 'array', true);
$arr2 = $arr[0]['string'];
$string = implode(", ",$arr2);
echo $string;
}
Thanks in advance for taking a look.
Topic array php custom-field Wordpress
Category Web