admin管理员组

文章数量:1122832

I have to change my all 3000 published post status to draft. one way to do it one by one or another option is to select bulk post... but bulk selecting also takes a lot of time because I have around 3000+ posts. any easy way to do it? or any plugin? Thank You

I have to change my all 3000 published post status to draft. one way to do it one by one or another option is to select bulk post... but bulk selecting also takes a lot of time because I have around 3000+ posts. any easy way to do it? or any plugin? Thank You

Share Improve this question asked Nov 16, 2021 at 10:40 The Coding BusThe Coding Bus 1113 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

If you have access to phpMyAdmin you can run this SQL query directly:

UPDATE {prefix}posts SET post_status="draft" WHERE post_status="publish"

If you do not have any experience with SQL queries then you should use this solution. Just place this code in functions.php and refresh the WordPress once, and it will be done.

function setPostsToDraft()
{
    global $wpdb;
    $qry = 'UPDATE '.$wpdb->prefix.'posts SET post_status="draft" WHERE post_status="publish"';
    $wpdb->query($qry);
}
setPostsToDraft();

While it's possible to use get_posts to retrieve and edit all posts like:

$args = array(
     'post_type'=> 'post',
     'post_status' => 'publish',
     'posts_per_page'=> -1
);

$posts = get_posts($args);

foreach($posts as $post){

   wp_update_post(array(

      'ID' => $post->ID,
      'post_status' => 'draft',
   ));  
}

if the number of posts is very large, running this function could overload your server, causing performance issues every time it is executed.

Also, depending on how you implement the function, you may need to edit your source code each time you want to use it.

To avoid these issues, I recommend using the Bulk Task Editor (BTE) plugin.

The plugin comes with a built-in feature for changing post status, along with other functionalities.

It also lets you save and reuse tasks as needed, since they are stored in the task list, making the process more efficient and sustainable.

Furthermore, you can create a custom task to apply an additional condition for altering the post status.

Here’s an example of how this can be accomplished:

Register the task

add_action( 'rewbe_post_type_actions', function($actions,$post_type){

     if( $post_type == 'your-custom-post-type' ){

          $actions[] = array(

               'label'  => 'Your custom task',
               'id'     => 'bulk_task_name',
          );
     }
     
     return $actions;
     
},10,2);

Add a callback

add_action('rewbe_do_post_{bulk_task_name}',function($post,$args){
     
     $custom_condition = false;

     // test your custom condition here using $post

     if( $custom_condition === true ){
          
          wp_update_post(array(

               'ID' => $post->ID,
               'post_status' => 'draft',
          ));
     }
     
     return $post;
     
},10,2);

Execute the Task

Access the Task Menu: Navigate to the “Tasks” section in the WordPress admin sidebar.

Create a New post Task: Click “Add New” to set up a new post task, and give it a descriptive name for easy identification later.

Filter Posts (Optional): If needed, apply filtering options to narrow down the list of posts.

Select Custom Action: Choose the “Your custom task” registered earlier from the dropdown menu.

Note: Adjust the number of users processed at once according to your server’s capacity.

Start the Process: Click “Publish” or “Update” to begin the task. The Bulk Task Editor will then automatically process the posts and update their status as specified.

References

  • How to implement a custom post task using hooks?
  • wp_update_post()
  • https://wordpress.stackexchange.com/a/426492/37412

本文标签: pluginsHow to change my 3000 Published post status to Draft using PHPMyAdmin