I am trying to: sum child posts values and save into parent post every time child is updated
Link to a page where the issue can be seen:
enlace oculto
I expected to see:
Sum of child post values
Total cost sum
Required payment sum
etc...
Instead, I got: nothing
I looked at the following posts:
1.
https://toolset.com/forums/topic/how-to-get-sum-of-values-in-all-child-posts
I was able to get sum as short code but not save it into the parent post after modifying the and adding the code:
update_post_meta($parent_post_ID, 'wpcf-sum-all-edu-plns-cst', $edu_total_sum);
i dont see the parent field updated.
also, the shortcode require the post ID to be inserted manualy but i want to get it dynamicly as the comment in the SR stated.
my code:
// get sum of education costs short code
add_shortcode('get_edu_total_sum', 'edu_total_sum');
function edu_total_sum($post_ID, $atts ) {
$postid = $post_ID;
$totl_edu_cost = 'total-education-cost'; //change to your numeric field without 'wpcf-'
$parent_slug = 'education-parent'; //change to your parent post slug (type)
$child_slug = 'education'; //change to your child post slug (type)
$edu_childargs = array(
'post_type' => $child_slug,
'numberposts' => -1,
'meta_key' => 'wpcf-' . $totl_edu_cost,
'meta_query' => array(array('key' => '_wpcf_belongs_' . $parent_slug . '_id', 'value' => $postid))
);
$edu_child_posts = get_posts($edu_childargs);
$edu_total_sum = 0;
foreach ($edu_child_posts as $edu_child_post) {
$edu_total_sum += get_post_meta( $edu_child_post->ID, 'wpcf-' . $totl_edu_cost , true );
}
return $edu_total_sum;
}
2.
https://toolset.com/forums/topic/sum-of-all-child-post-values-auto-updates-parent-field
i change the values to meet my needs i.e. parent post and child post are both created with toolset but this code didn't work either
3.
https://toolset.com/forums/topic/save-sum-of-child-field-in-parent-post/
again i modified the code to meet my need but it does't work as there is some syntax error in the code i could find.
$edu_child_posts = types_child_posts('education', array('_wpcf_belongs_education-parent_id' => $post_id));
$edu_total_sum = 0;
foreach ($edu_child_posts as $edu_child_post)
{
if(isset($edu_child_post->fields['total-education-cost]))
{
$edu_total_sum += $edu_child_post->fields['total-education-cost'];
}
}
$edu_parnt_id = wpcf_pr_post_get_belongs( get_the_ID(), 'education' );
update_post_meta($edu_parnt_id, 'wpcf-sum-all-edu-plns-cst', $edu_total_sum);
please note that i use the proper hook to call the function when saving a cred form. i just omited it here but its in my function.php
i looked at many other support tickets to try and resolve this with no success.
please advise,
thanks,
David