Skip Navigation

[Résolu] Calculate a sum from related/child posts

This support ticket is created Il y a 1 an et 9 mois. There's a good chance that you are reading advice that it now obsolete.
Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 0 réponses, has 1 voix.

Last updated by Luit Il y a 1 an et 9 mois.

Assigned support staff: Jamal.

Auteur
Publications
#1935207

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