Skip Navigation

[Escalated to 2nd Tier] Delete child posts when parent is deleted

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

This topic contains 8 replies, has 2 voices.

Last updated by Gavin 4 years, 8 months ago.

Assisted by: Nigel.

Author
Posts
#1314311

Tell us what you are trying to do?
When a user deletes a main 'event' entry on the frontend, I'd like all child posts to also be deleted automatically.

Is there any documentation that you are following?

Is this still up to date?

https://toolset.com/forums/topic/delete-child-posts-cascade-deletes/

add_action( 'delete_post', 'wpv_delete_child_posts', 10 );

function wpv_delete_child_posts( $pid ) {
global $wpdb;

//added the 'event' parent slug
$ids = $wpdb->get_results( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = '_wpcf_belongs_event_id' AND meta_value = '".$pid."'" );

if(isset($ids)){
foreach ($ids as $key) {
wp_delete_post($key->post_id, true);
}
}

return true;
}

What is the link to your site?
hidden link --- just a test site.

#1314459

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Gavin

That code is out-of-date, it refers to the way post relationships were stored prior to Types 3.

Updated code would be something like this:

function ts_autodel_child_posts( $post_id ){

    $parent_post_type = 'parent-slug';
    $child_post_type = 'child-slug';
    $relationship = 'relationship-slug';

    $thepost = get_post( $post_id );

    if ( $thepost->post_type == $parent_post_type ) {

        $child_posts = toolset_get_related_posts( 
            $post_id,
            $relationship_slug,
            array(
                'query_by_role' => 'parent',
                'role_to_return' => 'child',
                'limit' => 9999
            )
        );

        foreach ($child_posts as $child_post) {
            wp_delete_post( $child_post, true );
        }
    }
}
add_action( 'delete_post', 'ts_autodel_child_posts' );

You'll need to edit the slugs at the start of that code.

#1314481

Thanks Nigel - this doesn't appear to be working. I want this to work using the frontend, but I've also tried deleting the parent in the backend, too.

Here's the code I changed:
$parent_post_type = 'event';
$child_post_type = 'event-date';
$relationship = 'event-event-date';

I also tried this:

array(
'query_by_role' => 'event', //parent
'role_to_return' => 'event-date', //child
'limit' => 9999

When I click the 'Delete' link, it starts but hangs on 'Deleting..'.

If I refresh the page, the event is no longer shown on that single event frontend. It's also not showing in the list view. However, in this list view, the child post still shows.

In the backend, the parent event still exists, as do any children.

Thanks.

#1314485

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

OK, let me check on an actual site, I wrote it on the fly but I may have missed something, I'll get back to you soon.

#1314503

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Gavin

I did identify a typo in the code sample above, on line 13 it should be $relationship not $relationship_slug.

But I'm still having problems getting this to work.

The toolset_get_related_posts function doesn't work when triggered by the delete_post hook (or the slightly earlier before_delete_post hook). There is a basic problem using it in the backend where you need to trash the parent posts before deleting them, and when they are trashed, toolset_get_related_posts no longer gets the connections.

It is possible—in the backend—to get around this by using the wp_trash_post hook instead, to delete the child posts when the parent is trashed.

But that doesn't help in the front-end. When using the cred_delete_post_link shortcode to delete posts and relying on the delete_post hooks the toolset_get_related_posts function doesn't work, it returns an empty array.

I need to consult with the developers about this to find a way past this impasse, and I'll get back to you.

#1315635

No problem - I look forward to hearing more. Cheers.

#1316049

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Sorry, I'm still waiting for a reply, I'll nudge them again this morning.

#1316895

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Hi Gavin

The feedback is that the toolset_get_related_posts API function should be updated to make the status of the origin post more flexible so that it can include trashed posts.

It's a small change which they should be able to provide pretty quickly, and once they have I can update the code to get this working.

I'll let you know as soon as I can.

#1316963

Thanks Nigel - that's great news. Looking forward to it 🙂