0

I need to make all current posts to a category called "Past Posts". I could not find a way to do it. I have 250+ posts. So I can't do it manually.

1
  • You can Bulk edit and assign category from admin panel. I think that would be easy rather than writing scripts. Jun 19, 2015 at 8:52

1 Answer 1

0

First query all post

$args = array(
    'post_type'         =>'posts',
    'post_status'       =>'publish',
    'posts_per_page'    =>-1

);
$query = new WP_Query($args);
$post_ids = array();
if($query->have_posts()){
    while ($query->have_posts()){
        global $post;
        $query->the_post();
        array_push($post_ids,$post->ID);
    }
}

Now you have an array called $post_ids which contains all post's id .

Now , change the categories

Loop through the array and change the categories

$post_categories= array('Past Posts');
foreach ($post_ids as $key => $post_ID) {
    if(!in_category('Past Posts')){
        wp_set_post_categories( $post_ID, $post_categories, true );
    }
}

If you want to append a category then set the third parameter of wp_set_post_categories of this function true or if you want to replace then make it false .

Put this code in functions.php , you can delete this code after your work is done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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