Skip Navigation

[Resolved] Moving child posts to trash upon parent is sent to trash

This thread is resolved. Here is a description of the problem and solution.

Problem:

1. what hook can be used to trigger a function to send child posts to trash? Apparently wp_trash_post doesn't work here?

2. Upon upgrading Types to the latest release the following fragment wouldn't work (_wpcf_belongs is not part of the meta_key anymore?):

Solution:

Q1) Toolset form plugin is also using function wp_trash_post() to trash the post, so the wordpress action hook "wp_trash_post" should work too, more help:

https://developer.wordpress.org/reference/hooks/wp_trash_post/

Fires before a post is sent to the trash.

Q2) Yes, for the new post type relationship, please try the new Typers API function toolset_get_related_posts():

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

Query related posts by a set of conditions.

Relevant Documentation:

https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts

This support ticket is created 6 years, 4 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Hong_Kong (GMT+08:00)

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 6 years, 4 months ago.

Assisted by: Luo Yang.

Author
Posts
#922717

Hi,

I use cred_delete_post_link to remove parent post. Removed parent is sent to trash.
I'd like to send to trash also the child posts upon moving to trash the parent one.

My questions are:
1. what hook can be used to trigger a function to send child posts to trash? Apparently wp_trash_post doesn't work here?
2. Upon upgrading Types to the latest release the following fragment wouldn't work (_wpcf_belongs is not part of the meta_key anymore?):

 $child_posts = $wpdb->get_results( $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wpcf_belongs_xxx_id' AND meta_value = %d", $post_id ) );

I noticed some new table, toolset_associations, where parents and children are stored, so the above piece could look like:

$child_posts = $wpdb->get_results( $wpdb->prepare( "SELECT post_id FROM $wpdb->posts WHERE post_id IN ( SELECT child_id FROM $wpdb->toolset_associations WHERE parent_id = %d )", $post_id ) );

Is it ok or should I use another db table? I couldn't find any reference to that issue.

#922856

Hello,

Q1) Toolset form plugin is also using function wp_trash_post() to trash the post, so the wordpress action hook "wp_trash_post" should work too, more help:
https://developer.wordpress.org/reference/hooks/wp_trash_post/
Fires before a post is sent to the trash.

Q2) Yes, for the new post type relationship, please try the new Typers API function toolset_get_related_posts():
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/#toolset_get_related_posts
Query related posts by a set of conditions.

#923218

Thank Luo.

#923244

You are welcome