Scenario:
I have 1000 posts that have the “Uncategorized” category, and I want to remove “Uncategorized” from all of those posts and set a different category for those posts.
In other wordsâ take all Uncategorized posts and move them to another category, in one fell swoop.
Can I do this in bulk without going through each post individually?
What you are looking for is the WordPress bulk editor.
Go to Posts > Categories > Uncategorized
Click the “Screen Options” tab in the top right corner, then change “Number of items per page:” to 1000. (If you are on a really slow server you might consider doing less at a time)
Now select all of the items on the page and click the “Bulk Actions” drop-down above the select all and select the “Editâ option.
Hit Apply
In the bulk editor click the ânew categoryâ you want to change all of the posts to and hit update.
Once you have added all of the posts to the ânew categoryâ you need to remove the âUncategorizedâ category. To do this:
Once you delete the âUncategorizedâ category it will remove it from all of your posts.
If you have some posts that you want to remain in âUncategorizedâ then create a new category called âtempâ and assign all of the posts you want to remain to that category. Once you delete âUncategorizedâ create it again and assign the posts in âtempâ back to that category.
As you’ve discovered, the bulk editor only allows the ADDITION of categories to multiple posts – it’s not possible to REMOVE categories from multiple posts. The best option i found was to temporarily install this plug-in https://wordpress.org/plugins/bulk-remove-posts-from-category/ (in the WP repository) which adds the ability to REMOVE categories from multiple posts using the same bulk edit method. it simply adds an additional ‘remove’ checkbox under the category list.
The uncategorized category has an ID of
1
. What our worksflow will be is,Get all posts which is assigned to the uncategorized category.
We will only get post ID’s which will make our query up to 1000 times faster on a site with thousands of posts. This will also help that our query does not time out or hit a maximum memory fatal error.
Use
wp_set_object_terms()
to remove and set our new temsNOTE:
The code below requires PHP 5.4+ and any changes will be non reversable, so back up your database first
Note, nowhere have we changed the
$post
global or setup postdata, so we don’t need to callwp_reset_postdata()
🙂WordPress stores the parent/child relationships between categories and posts in the
wp_term_relationships
table, which is documented here. As @Pieter Goosen noted, the “Uncategoried” category has a ID of1
. So you can backup your SQL database, then connect to it with a SQL command line client (sudo mysql wordpress
works for me), and run this SQL command:If you want to move all uncategorized posts to another category, you can use the Bulk Move plugin.
If you want to remove a category from some posts, Bulk remove posts from category might be a better option.
As Melebius said, it is much, much quicker (and easier) to use https://wordpress.org/plugins/bulk-remove-posts-from-category/ ! It works with products as well as posts. Brilliant!
You can add this to your theme’s functions.php file, then refresh any page on your site once, then remove the function.
Use at your own risk and backup the database first! There’s no UNDO button on this.