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
#1935129

I am trying to achieve the same calculation as in https://toolset.com/forums/topic/calculating-the-sum-of-custom-field-values-across-multiple-child-posts/

I have a CPT 'persoon' with childs of post type 'phv'.

I have rewritten the php code to my site but still get a result of 0. I think it is not getting the post id of CPT persoon. I use the shortcode in CPT Persoon.

Code
add_shortcode('sum_of_lengte', 'sum_of_lengte_func');
function sum_of_lengte_func(){
$persoon_id = get_the_ID();
$children = get_posts(
array(
'post_type'=> 'phv',
'meta_query' => array(
array(
'key' => '_wpcf-belongs_persoon_id',
'value' => $persoon_id,
),
),
'fields' => 'ids'
)
);
$sum = 0;
foreach($children as $id){
$sum += get_post_meta( $id, 'wpcf-phv_lengte', true);
}
return $sum;
}