Skip Navigation

[Resuelto] sum child posts values and save into parent post every time child is updated

This support ticket is created hace 7 años. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Este tema contiene 1 respuesta, tiene 1 mensaje.

Última actualización por davidZ-4 hace 7 años.

Autor
Mensajes
#587791

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

#587882

I was able to solve this by looking at https://toolset.com/forums/topic/sum-of-fields-in-a-view-2

Thanks,

David