HI Jamal, I rewrote the code but I am gettings some errors.
The code
add_shortcode('sum_of_lengte', 'sum_of_lengte_func');
function sum_of_lengte_func(){
$origin_id = $atts['persoon'];
$relationship_slug = 'relatie_personen_phvs';
$child_posts = toolset_get_related_posts(
$origin_id, // get 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.
)
);
$sum = 0;
foreach($child_posts as $id){
$sum += get_post_meta( $id, 'wpcf-phv_lengte', true);
}
return $sum;
}
I want to sum the field wpcf-phv_lengte which is a field of child post type 'phv' with cpt 'persoon' as parent