0

For one of my project I just want to change the post status of all the posts inside a specific category from publish to pending.

It is possible to change status of multiple posts at once? I want to use this functionality in a custom theme I am developing.

thanks

2 Answers 2

1

I am sure there's an easier way to accomplish this, but this how I would've done.

  1. Query all posts with category X and status X
  2. Update query results with status Y.

    $target_category = 'news';
    $change_status_from = 'draft';
    $change_status_to = 'publish';
    $update_query = new WP_Query(array('post_status'=>$change_status_from, 'category_name'=>$target_category, 'posts_per_page'=>-1));
    
    if($update_query->have_posts()){
    
        while($update_query->have_posts()){
    
            $update_query->the_post();
            wp_update_post(array('ID'=>$post->ID, 'post_status'=>$change_status_to));
    
        }
    
    }
    

You can place this code inside a page template, or in functions.php. It's likely that you only need to run it every now and then. So I would create a template for it. Add it to the template and then assign that template to a specific page that's maybe marked as private or draft.

2

Or you can do this in MySQL using a query such as:

UPDATE tb_posts 
SET post_status = 'pending' 
WHERE ID IN ( 
    SELECT object_id FROM `tb_term_relationships` 
    WHERE term_taxonomy_id = {your_cat_id} 
)

If you hover over a category on the category page, you can see its ID in the URL

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.