• Hello,

    I’m using toolset to create custom post type fields and custom posts. I have a PHP code in a custom function.php that run some calculations using a function to the parent and a function to the child every time the post is saved (and its working just fine).

    so far I couldn’t get the toolset support to resolve my issue (below) as they see it as a noon plugin issue (custom code).

    I want to update the child post calculations when the parent post value is changed and saved.
    my thought was to do the following:

    
    function calculate_parent ($post_ID) {
    run calculation code // this is working fine and no need to debug it
     
    // update child post from inside the parent function
    run the code to find the child posts // I have this in place
    call the child function to run on the child post by providing the child id (I think) // this is where the issue is
     
    }
    
    function calculate_child ($post_ID) {
    run child calculation by get parent custom post fields values // this is working fine
    }
     

    for example I have the code below inside the function that run after the parent post is updated but i’m not sure if its correct as I dont see the child post updated correctly.

    
    // find child post - life-insurance-need type and update
            $child_posts = types_child_posts('life-insurance-need');
                foreach ($child_posts as $child_post) {
                $post_ID = $child_post->ID;               
                calculate_li_need ($post_ID);
            }
    

    I’m not sure if this is how to call the child_calc function to run from the parent.

    any thoughts?

    thanks,

    David

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • You could use the save_post hook and run it only for top level pages. If you use update_post_meta() in this hook, you’re fine but if you need to use wp_update_post() you’ll need to unhook the action:

    remvoe_action( 'save_post', 'child_theme_update_children' );
    wp_update_post( $args );
    add_action( 'save_post', 'child_theme_update_children' );

    Here’s what the full code could look like:

    /**
     * Update Children
     *
     * @param Integer $post_id
     * @param WP_Post Object $post
     *
     * @return void
     */
    function child_theme_update_children( $post_id, $post ) {
    
    	// If we're not in the right place, bailout
    	if( ! isset( $post ) || wp_is_post_autosave( $post ) || wp_is_post_revision( $post ) ) {
    		return;
    	}
    	
    	// Ensure we only run this for pages
    	if( 'page' == $post->post_type ) {
    
    		// Is a top level page
    		if( 0 === $post->post_parent ) {
    		
    			$children = get_posts( array(
    				'post_type'			=> 'page',
    				'post_parent'		=> $post->post_parent,	// Only pull pages with this parent
    				'posts_per_page'	=> 100,			// Get up to 100 pages
    				'fields'			=> 'ids',		// Only Return IDs
    			) );
    			
    			
    			
    			if( ! empty( $children ) ) {
    			
    				foreach( $children as $child_id ) {
    					
    					$lawl_idk = calculate_li_need( $child_id );
    					
    				}
    			
    			}
    		
    		}
    		
    	}
    	
    }
    add_action( 'save_post', 'child_theme_update_children', 20, 2 );
    • This reply was modified 7 years, 4 months ago by Howdy_McGee.
    Thread Starter davidzohar

    (@davidzohar)

    @howdy_mcgee
    thanks for the quick response on this.
    my scenario is a bit different as I work on custom post types so I had to modify the code a bit.
    for some reason this was not working for me. instead its acting as if the parameters from the parent are zero and the calculation return zeros instead of the actual calculation.
    I dont understand why as when I save the child post using the add action save_post the calculation is running correctly but when I try to run it from inside the parent post function I get zeros…

    my code inside the parent function at the end of it is:

    
    // update li child with parent values
    	//Parent Post ID
    	$networth_parnt_id = get_the_ID();	
    	// get all LI child posts related to the parnet
            $li_chld_posts = get_posts(array(
                'numberposts'   => -1,
                'post_type'     => 'life-insurance-need',
    			'meta_query' => array(array('key' => '_wpcf_belongs_net-worth_id', 'value' => $networth_parnt_id))
    			'fields'			=> 'ids',
                )
            );
    		//if it returns some LI posts
            if( $li_chld_posts ){
                 //now get the single LI posts fields values 
                foreach( $li_chld_posts as $li_single_post ){
                    //get each LI post data
                    $sngl_li_data = get_post($li_single_post);
                    //get each ID
                    $sngl_li_id = $sngl_li_data->ID;
                    //get each posts field value
                    //run the child claculation by calling the child calc fucntion
                    $lawl_idk = calculate_li_need( $sngl_li_id );
                }               
            }
    

    any thoughts?

    David

    • This reply was modified 7 years, 4 months ago by davidzohar.

    I’m not entirely sure either, it’ll take some debugging on your part to come to a final solution since it seems very specific to your use case. A couple things to note: 1) Your metaquery parameter is missing a comma at the end of the statement, it should look like this:

    $li_chld_posts = get_posts( array(
    	'numberposts'   => -1,
    	'post_type'     => 'life-insurance-need',
    	'meta_query' 	=> array( array( 'key' => '_wpcf_belongs_net-worth_id', 'value' => $networth_parnt_id ) ),
    	'fields'		=> 'ids',
    ) );

    It’s also worth noting that what is being returned are Post IDs. So when you run your foreach() you are looping over post IDs:

    foreach( $li_chld_posts as $sngl_li_id ) {
    	$lawl_idk = calculate_li_need( $sngl_li_id );
    }

    This is what the 'fields' => 'ids' parameters tells WordPress to return, an array of post ids.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘call child post calc function when parent post value update’ is closed to new replies.