Skip Navigation

[Résolu] Delete child posts on deleting parent

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

The issue here is that the user wanted to delete their child posts once the parent is deleted on the frontend.

Solution:
This can be done by using the hook below.

add_action( 'before_delete_post', 'ts_before_delete_post', 1, 2 );
function ts_before_delete_post( $post_id, $post ){
  
    if ( $post->post_type == 'persoon' && $post->post_status == 'publish' )
    {       
        $children = toolset_get_related_posts( $post_id,'relatie_personen_phvs',array('query_by_role' => 'parent','role_to_return' => 'child'));
  
        if ( is_array( $children ) )
        {
            foreach ($children as $key => $child) {
                wp_delete_post( $child, true );
            }
        }
    }
}

Add it to your Toolset custom code settings in Toolset->Settings->Custom Code. Ensure that you have activated it.

Next you will need to replace 'persoon' with the Parent Post type and "relatie_personen_phvs" with the post relationship slug.

This will ensure that the child posts are deleted when the parent is deleted.

Finally this will only work if the action of the toolset delete button is set to 'delete'. This will not work when it is set to 'trash'

This support ticket is created Il y a 3 années et 1 mois. 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Marqué : 

This topic contains 9 réponses, has 2 voix.

Last updated by Luit Il y a 3 années et 1 mois.

Assisted by: Shane.

Auteur
Publications
#1964015

I created a snippet based on https://toolset.com/forums/topic/delete-related-posts-and-their-attachments-when-deleting-parent-post/

I use a delete post shortcode in a edit form to edit CPT 'persoon':

[cred_delete_post_link class='cred-refresh-after-delete, btn btn-primary btn-lg' text='Verwijderen' action='trash' message='Wil je deze persoon verwijderen?' redirect='3630' ]

I want all releated childs of CPT phv also deleted when deleting the parent persoon. The relationship slug is 'relatie_personen_phvs'

I use this snippet:

function delete_post_children($post_id) {

if( $post_type == 'persoon' ) {
// in this case, we want to force-delete the child phv posts as well
$phv_persoon = toolset_get_related_posts(
$post_id,
'relatie_personen_phvs',
[
'query_by_role' => 'parent',
'role_to_return' => 'child',
'limit' => 10000,
'orderby' => null,
]
);
foreach($phv_persoons as $phv_persoon ) {
wp_delete_post( $phv_persoon, true );
}
}
}

add_action('before_delete_post', 'delete_post_children');

But nothing happens, i am not sure if it's even using this function.

#1964241

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Luit,

Thank you for getting in touch.

Would you mind allowing me to have access to the site so that I can see what happens when you try to delete the posts?

I've enabled the private fields for your next response. Also please send me a link to an example page that has children that can be used as a test.

Thanks,
Shane

#1964655

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Luit,

I was able to perform a test on this with a sample post ive created and it works fine on my end now.

add_action( 'before_delete_post', 'ts_before_delete_post', 1, 2 );
function ts_before_delete_post( $post_id, $post ){
 
    if ( $post->post_type == 'persoon' && $post->post_status == 'publish' )
    {       
        $children = toolset_get_related_posts( $post_id,'relatie_personen_phvs',array('query_by_role' => 'parent','role_to_return' => 'child'));
 
        if ( is_array( $children ) )
        {
            foreach ($children as $key => $child) {
                wp_delete_post( $child, true );
            }
        }
    }
}

Also I've added the delete button back at the bottom of the page as a test and it works. What you can do is to setup a sample post on your end with a few children and test to see if you get it to work as well.

Thanks,
Shane

#1964745

I created a parent post and some children posts and tried to delete them with the delete button on the parent edit form, but the children are still there. So it's not working for me.

#1965549

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Screenshot 2021-02-24 at 9.17.52 AM.png

Hi Luit,

It actually works perfectly fine for me.

Take for instance this post here.
hidden link

It has this child post here.
hidden link

If you click the delete button at the bottom of the page, the child will get deleted. See Screenshot.

Also notice in the button shortcode that i've set it to delete the post and not trash it.

[cred_delete_post_link class='cred-refresh-after-delete, btn btn-primary btn-lg' text='Verwijderen' action='delete' message='Wil je deze persoon verwijderen?' redirect='3630' ]

You can go ahead and test the post that I've created and you'll see that both items get deleted.

Thanks,
Shane

#1965587

Hello Shane, I use a delete button on the edit form for the person. The delete button on the post it self is working and is deleting the child posts.

So at the top of the post there is a button Bewerken to edit the person. At the bottom of this form is the delete button I want to use. This one has exactly the same shortcode and parameters but is only removing the parent, not the children.

#1965593

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Luit,

From my previous post I mentioned that I had to change the action from trashing the post to delete the post.

Currently on the form you have this.

action='trash'

However what is required is action='delete'. The hook "before_delete_post" doesn't work for when a post is being trashed. This hook only fires when the post is deleted directly.

Changing your action to delete should solve the issue.

Please let me know if this clarifies the issue for you.

Thanks,
Shane

#1965629

Yes indeed, that works. What would I have to change in order to trash the parent and childs instead of a permanent delete?

#1965681

Shane
Supporter

Languages: Anglais (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Luit,

Unfortunately the hook that would fire for the trashing of posts only works it is done from the wordpress admin page as per my colleagues investigation
https://toolset.com/forums/topic/delete-child-posts-when-parent-post-is-deleted/#post-1883945

Only the deleting of the posts will fire the hook so that you can delete the child posts as well. This is a limitation of wordpress and not our plugins themselves since we are piggy backing on their hook to get this done.

SO the only way to get this to work is to delete the posts rather than trashing them.

Thanks,
Shane

#1965699

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.