0

I try to get a list for all posts of custom post type bedrijf from all values of a custom field. This field is an array of values. However, when I try to output the custom fields, I get

Notice: Array to string conversion in ...

I have tried different approaches like foreach, wp_pluck_list, but so far, no success.

function make_list_regios(){
global $post;
$output = '<ul>';
$post_idees = new WP_Query(array('post_type' => 'bedrijf'));
$values = wp_list_pluck( $post_idees->posts, 'ID');
foreach( $values as $value ) {
$meta_values[] = get_post_meta( $value, 'elements', true );
            $output .= '<li>'.$meta_values.'</li>';
}

return ($output);
}
add_shortcode('regios', 'make_list_regios');

When I print_r($values), it is an array of post_id. When I print out $meta_values, I get an array as follows:

Array ( [0] => [1] => Array ( [0] => groningen [1] => limburg ) [2] => [3] => [4] => Array ( [0] => noordholland [1] => zuidholland ) ) 

What I want to output is a full list of selected regions.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
Webdever
  • 485
  • 5
  • 17

2 Answers2

0

It seems your array have null values and you cannot echo array element. Use json_encode

$output .= '<li>'.json_encode(array_filter($meta_values)).'</li>';

Or Implode array elements

$meta_values = array_filter($meta_values);
$meta_string = implode(', ', $meta_values);
$output .= '<li>'.$meta_string .'</li>';
Sangita Kendre
  • 429
  • 4
  • 11
0

With the help of @j. scott elbein i finally solved this one. The correct code (in my case at least) is:

function make_list_regios(){
global $post;
$output = '<ul>';
$post_idees = new WP_Query(array('post_type' => 'bedrijf'));
$values = wp_list_pluck( $post_idees->posts, 'ID');
foreach( $values as $value ) {
            $meta_values[] = get_post_meta( $value, 'elements', true );
            $meta_values2 = array_filter($meta_values);
            $meta_values3 = call_user_func_array('array_merge', $meta_values2);
}
foreach($meta_values3 as $meta_value3){
            $output .='<li>'.$meta_value3.'</li>';
            }
            $output.='</ul>';
            return ($output);
}
add_shortcode('regios', 'make_list_regios');
Webdever
  • 485
  • 5
  • 17