3

I'm trying to delete all child posts when the parent is deleted.

The parent post deletes just fine, but the child posts are not properly deleting.

Here's the code I have in place now:

$args = array( 
        'post_parent' => $parentid,
        'post_type' => 'custom-type'
    );

    $posts = get_posts( $args );

    if ($posts) {

        // Delete all the Children of the Parent Page
        foreach($posts as $post){

            wp_delete_post($post->ID, true);

        }

    }

    // Delete the Parent Page
    wp_delete_post($parentid, true);

What it should do is loop through and get the posts that are the child of $parentid and delete them, then delete the parent post.

Currently, it just deletes the parent post, but leaves all the children behind.

I'm looking at my database and the child pages are definitely created correctly with the correct post_parent id.

Is there a way to get all child posts and delete them?

Thanks,

Jason

2 Answers 2

3

Try it like this:

$args = array( 
    'post_parent' => $parentid,
    'post_type' => 'custom-type'
);

$posts = get_posts( $args );

if (is_array($posts) && count($posts) > 0) {

    // Delete all the Children of the Parent Page
    foreach($posts as $post){

        wp_delete_post($post->ID, true);

    }

}

// Delete the Parent Page
wp_delete_post($parentid, true);
8
  • This worked great! Thanks so much!!! Mind sharing why this worked and not the way I had it before, just so I can actually have an understanding?
    – Jason Bahl
    Jun 4, 2012 at 19:24
  • Actually, I lied. It worked once, but now it's not working again.
    – Jason Bahl
    Jun 4, 2012 at 19:45
  • Of course! The get_posts function returns either an array (if there are results) or null. You could also write if($posts != null){ but I prefer writing conditional statements that check what you DO expect than on what you DON'T expect (in case WP changes in the future and starts returning something different on no result). Glad I could help! Jun 4, 2012 at 19:45
  • What do you mean with 'worked once'? Once every session? Jun 4, 2012 at 19:48
  • I created a parent post and 3 child posts, then ran the delete function and it worked. Then I created a parent again with 3 more child posts and it only deleted the parent page again.
    – Jason Bahl
    Jun 4, 2012 at 19:57
0

Hook into delete_post. The code below should run recursively and will delete if there are grandchilds too. code not tested

add_action('delete_post', 'wpse53967_clear_all_childs');

function wpse53967_clear_all_childs($post_id){
    $childs = get_post(
        'post_parent' => $post_id,
        'post_type' => 'post-type' 
    );

    if(empty($childs))
        return;

    foreach($childs as $post){
        wp_delete_post($post->ID, true); // true => bypass trash and permanently delete
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.