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;
}