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 have a category with about 3.500 posts. 2.500 are not published, but saved as drafts.

I want to unpublish all the remaining 1000 posts from this one category. Maybe I will want to delete all of the posts later on.

How can I do this?

share|improve this question

1 Answer 1

You need to get the published posts in the category then run wp_update_post() inserting the new status. Here is a quick function. I am attaching it to admin init. I would also turn it off after your statuses are updated.

function wpse_55018_get_update() {
   for( $i = 0; $i < 10; $i++ ) {     // Runs this loop 10 times
       $args = array(
          'post_status' => 'published',
          'cat'         => 23,        // Use Category ID here.
          'posts_per_page' => 100,    // Lets just do 100 at a time so we don't blow up your server
          'no_found_rows'  => true,   // We don't need to paginate.
          'update_post_meta_cache' => false,  // We don't need post meta either. 
   );
    $posts = get_posts( $args );
    if ( !$posts ) return;             // Breaks out of the function if there are no posts.
        foreach ( $posts as $post ) {
        print_r( wpse_54018_change_status( $post->ID ) );

        }
    }
}

 functionwpse_54018_change_status( $post_id ) {
    $updated_post = array();
    $updated_post['ID'] = (int) $post_id;
    $updated_post['post_status'] = 'draft';

    wp_update_post( $updated_post );
        return $post_id .' Has been Updated';
    }


add_action( 'admin_init', 'wpse55018_get_update' );  This will run the next an admin page is loaded.  
share|improve this answer
    
This does not update the posts one by one. This runs a loop 10 times that updates 100 at a time. –  Chris_O Jun 2 '12 at 5:16
    
oups... accidentally deleted my first comment :). Yes you fetch 100 posts by a time, but you update only one by one, when you call wp_update_post function. –  Eugene Manuilov Jun 2 '12 at 5:22
    
which file should I add this code to? –  Anders Jun 4 '12 at 17:26

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.