Hello, there could be one or more problems here. First, I would try to determine if the toolset_get_related_posts query is returning the correct values. To test that, I would try this code modification:
add_shortcode('display_fields', 'display_fields_func');
function display_fields_func ($atts){
$origin_id = get_the_ID(); // Post id of the parent post of type 'persoon'
$relationship_slug = 'relatie_personen_phvs'; // relationship slug between persoon and phv
$child_posts = toolset_get_related_posts(
$origin_id, // get childs posts connected to this one
$relationship_slug, // in this relationship
array(
'query_by_role' => 'parent', // origin post role
'role_to_return' => 'child', // role of posts to return
'return' => 'post_id', // return array of IDs (post_id) or post objects (post_object)
'limit' => 999, // max number of results
'offset' => 0, // starting from
'orderby' => 'title',
'order' => 'ASC',
'need_found_rows' => false, // also return count of results
'args' => null // for adding meta queries etc.
)
);
// test child_posts array
$child_ids = implode(', ', $child_posts);
return $child_ids;
foreach($child_posts as $id){
$array = get_post_meta( $id, 'wpcf-phv_lengte');
$list = implode(', ', $array);
}
return $list;
}
The shortcode should now return a comma-separated list of child post IDs, like this:
Each number should represent one child post. If your parent post has three child posts, you should see three child IDs output by the shortcode. If you do not see the three child post IDs, you know there is a problem with the post relationships API query. If you see the correct child post IDs, you know the problem is in the foreach loop after the query.
Once the results of the post relationships query are confirmed, you can delete the code I added:
// test child_posts array
$child_ids = implode(', ', $child_posts);
return $child_ids;
In the foreach loop, it looks like you are resetting the values of $array and $list each time the loop iterates. It seems like you should be appending a value to $array in each loop iteration, then you should implode the list AFTER the loop is complete. Something like this:
// initialize an empty holder array
$array = array();
foreach($child_posts as $id){
$array[]= get_post_meta( $id, 'wpcf-phv_lengte');
}
$list = implode(', ', $array);
return $list
But this depends on whether or not the phv_lengte field allows multiple values. I cannot tell exactly what you're trying to achieve here based on the information I have.