Skip Navigation

[Résolu] calculate parent post after child post was deleted

This support ticket is created Il y a 7 années. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

Ce sujet contient 14 réponses, a 2 voix.

Dernière mise à jour par davidZ-4 Il y a 7 années.

Assisté par: Minesh.

Auteur
Publications
#590014

Tell us what you are trying to do? calculate parent post after child post was deleted

Is there any documentation that you are following?
https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_delete_post_link
https://toolset.com/forums/topic/cred-redirect-to-parent-after-delete/

Is there a similar example that we can see?
no

What is the link to your site?
lien caché

I would like to have a function in my function.php that will run after deleting a child post
I know how to calculate the parent post but not sure how to call the calculation after the child post was deleted and run the update calculation on the parent.

Please advise,
thanks,

David

#590088

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - It seems to be not logical to count parent posts when you delete any child post as you are actually deleting child post and it will not have any effect on parent post count.

Did I misunderstand you? If yes: could you please share more details about your issue.

#590260

Hi Menish,

yes, you are right with your statement however you misunderstood my scenario so I will try to explain again.

I have a custom parent post type called 'total educations cost'
each parent can have many custom child post called 'education'
when I create the child custom post I sum a few values from all child's that belong to the parent and populate the parent post with the values. think of it as child education cost as follow:
child 1 - $1,000
child2 - $1,500
Parent post cost sum will be $2,500
I know how to do it, and I have a function in my function.php that do it and its working just fine.

the issue is when I want to delete a child (in the front end), which I can do using the cred_delete_post_link shortcode, is that the parent values are staying the same as if the child I deleted was never deleted. this is normal and expected as nothing was updating the parent post (recalculation).

so, what I need is a way to recalculate the parent post values when a child post was deleted. or in other words call a function that will calculate the parent when a child was deleted.

I hope this is more clear about my scenario.
Thanks,

David

#590432

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Ok I got it. You would like to update the parent post calculation.

Well - you should use the hook 'post_updated' and check if "post_status" changed to trash and if deleted post belongs to your child post type 'education' , then run your code.

For example:

function func_update_prent_post_value($post_ID, $post_after, $post_before){
 
          if($post_after->post_status == 'trash' and $post_before->post_type=='education'){
                // find your parent post ID based on $post_ID and update the parent field value as required
          }
 
 
}
add_action( 'post_updated', 'func_update_prent_post_value', 10, 3 ); 

More info:
=> https://codex.wordpress.org/Plugin_API/Action_Reference/post_updated

If you still do not have idea how to update parent post field value - please do not hesitate to get in touch with me.

#590436

Thanks Minesh,
this is great I will sure write my function based on this.

one question:
I see at https://toolset.com/documentation/user-guides/cred-shortcodes/#cred_delete_post_link that there is a deference between 'trash' and 'delete' action of the shortcode.

If I use delete in the shortcode this will permanently delete the post and the code above might not catch it. correct?
do I have to use 'trash' in the delete short code in order to make the code sample above work?

David

#590440

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Ok - If you would like to set 'delete' action than you should try before_delete_post action.

More info:
https://codex.wordpress.org/Plugin_API/Action_Reference/before_delete_post

But you may check if "post_updated" hook still works with delete action, if so - all fine 🙂

#590443

Ok, great,
Thanks,

I will check it out and let you know if I need more help.

thanks,

David

#590444

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Ok fine. Please feel free to get in touch with me with your queries, if any.

#590674

Hi Menish

I tried the code below with no success.

function update_edu_prent_educhld_del($post_ID, $post_after, $post_before){
          if($post_after->post_status == 'trash' and $post_before->post_type=='education'){
	//Parent Post ID
	$edu_parnt_id = wpcf_pr_post_get_belongs( get_the_ID(), 'education-parent' );	
	// get all education child posts related to the parnet
        $edu_chld_posts = get_posts(array(
            'numberposts'   => -1,
            'post_type'     => 'education',
'post_status' => 'publish',
			'meta_query' => array(array('key' => '_wpcf_belongs_education-parent_id', 'value' => $edu_parnt_id))
            )
        );
		//if it returns some education posts
        if( $edu_chld_posts ){
            //get the single education posts fields values 
            foreach( $edu_chld_posts as $edu_single_post ){
                //get each education post data
                $sngl_edu_data = get_post($edu_single_post);
                //get each ID
                $sngl_edu_id = $sngl_edu_data->ID;
                //get each posts field value
                $sngl_edu_ttlcost_value = get_post_meta($sngl_edu_id, 'wpcf-total-education-cost', true);
                //Sum the values all posts fields
                $edu_totl_cost_sum+= $sngl_edu_ttlcost_value;
            }               
        }
		update_post_meta($edu_parnt_id, 'wpcf-sum-all-edu-plns-cst', $edu_totl_cost_sum);
          }
 
}
add_action( 'post_updated', 'update_edu_prent_educhld_del', 10, 3 ); 
}

I also tried with the following way to get the parent id with no success.

$edu_parnt_id = get_post_meta( $post_id, '_wpcf_belongs_education-parent_id', true);

please advise,
thanks,

David

#591139

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Could you please share problem URL where I can see delete post link you added and few test posts to delete.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#591486

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Thanks for the information but first of all, could you please move all code to your current theme's functions.php file.

#591488

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Thanks for the information but first of all, could you please move all code to your current theme's functions.php file.

#591623

Hi Minesh,
I have moved custom code to the theme function.php.

FTP note: please dont use a folder name in the ftp link. the folder name is a mandatory field in toolset so I wrote root folder but you dont need it so the ftp is the domain name I provided (including the subdomain name)...

Thanks,
David

#592091

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Well - there is silly issue and when you see it I bet - you must have a laugh.

You said:

also tried with the following way to get the parent id with no success.

$edu_parnt_id = get_post_meta( $post_id, '_wpcf_belongs_education-parent_id', true);

So - if you look at your function - it has argument passed as $post_ID

function update_edu_prent_educhld_del($post_ID, $post_after, $post_before){

Now, within the function you are using $post_id :

$edu_parnt_id = get_post_meta( $post_id, '_wpcf_belongs_education-parent_id', true);	

I've changed the $post_id to $post_ID and I can see the right parent ID and your calculation must work fine.

$edu_parnt_id = get_post_meta( $post_ID, '_wpcf_belongs_education-parent_id', true);	

Could you please confirm. 🙂

#592177

Oh, yes this makes me laugh and cry at the same time 🙂
thank you so much for helping with this it works perfectly!

Thanks,

David