Bulk WordPress Category Cleanup with MySQL

In my previous post I detailed how I imported 10 years of content from a BlogEngine.NET blog to WordPress.  Making a fresh start in WordPress I thought it was a good time to address the hundreds of uncategorized posts in my original blog. Nearly all of the 366 uncategorized posts from my prior blog were written between 2003 and 2005 and were migrated to two different .NET blogging apps before finding their way to WordPress, so there were bound to be some missing details over the years.

To get the job done quickly I turned to MySQL. I could have gone into WordPress and used Quick Edit, but not for 366 posts! And the WordPress Bulk Actions option?  A couple of problems with that. First, it’s a page-by-page operation (though with WordPress Screen Options and increasing page size that could have been mitigated), but the main reason was that bulk actions category assignment will ADD a category but not REMOVE the “Uncategorized” assignment on each post.

Looking back, I probably could have used bulk assignment and then gone into MySQL and done a single query to delete the “uncategorized” records. Something to consider for next time, though I got to spend some quality time in MySQL using my approach. :)

The MySQL

We’re going to be updating the table wp_term_relationships, changing the term_taxonomy_id from “1” to the category we want.  We’ll begin by viewing the category ids in wp_terms.

Now onto wp_term_relationships where our object_id is the post_id we’ll be using in our update query.

We’re going to view the title of the uncategorized posts, which gives us sufficient info to assign a new category.

Entering the IDs in our update statement is not something we want to do so we’re going to output the wp_posts IDs to a CSV file.

Before going further, we do NOT need to wrap the IDs in single quotes. I simply wasn’t thinking. No problem and no additional time spent, but not necessary.  Here’s the .CSV in gedit.  The idea is to scan the post titles, then cut and paste into our sql query. This works well since nearly all of my uncategorized posts were on a few subjects, mostly .NET back then.

Now our MySQL update query resulting from our title scan and gedit ID segment cut-n-paste.

I should mention that with MySQL you can open multiple instances and windows for viewing various reference data. Here’s my Linux desktop with multiple MySQL windows.

Changing the Uncategorized Post Count

The sql work did the job, but one thing left to do, update the number of posts which are recorded for the “uncategorized” category.  Otherwise, the Uncategorized category will continue to display in a widget and no posts will display in the category.  Changing the number of posts in wp_term_taxonomy to “0” will fix that.