Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

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);
share|improve this answer
    
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 '12 at 19:24
    
Actually, I lied. It worked once, but now it's not working again. –  Jason Bahl Jun 4 '12 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! –  Barry Kooij Jun 4 '12 at 19:45
    
What do you mean with 'worked once'? Once every session? –  Barry Kooij Jun 4 '12 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 '12 at 19:57
show 3 more comments

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
    }
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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