0

There is a function that removes all the posts from a given post type but it doesn't remove items that status is "draft". How would it catch the draft items too?

   function remove_all_kba_data(){
        //remove articles
        $articles = get_posts( array( 'post_type' => 'kb_kba', 'posts_per_page' => -1) );
        foreach( $articles as $article ) {
            //delete post, bypass trash
            wp_delete_post( $article->ID, true);
        }
    }

1 Answer 1

3

get_posts() can receive an argument describing which post status to operate. I would say something like this:

function remove_all_kba_data(){
    //remove articles
    $articles = get_posts( 
        [ 
            'post_type' => 'kb_kba', 
            'posts_per_page' => -1, 
            'post_status' => [ 'published', 'draft' ]
        ] 
    );
    foreach( $articles as $article ) {
        //delete post, bypass trash
        wp_delete_post( $article->ID, true);
    }
}
1
  • for my site the post_status needed to be 'publish' rather than 'published' Sep 3, 2021 at 18:20

Your Answer

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

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