Personal Tech Takes

Converting Tags To Categories In WordPress

Spent the last two days working furiously, integrating more plugins, checking whether they work, throwing exceptions, adding Google AdSense ads, et al. Which reminds me, I wanted to add Yahoo! Publisher Network ads – apparently, they pay more, allow you to receive payments via PayPal, and even let you add ads to PDF files and RSS feeds – BUT, and this a big pain in the butt, they want US-based publishers only with predominantly US-based traffic (otherwise, they even ban people…brr).

The Error
One problem I encountered immediately after shifting was using the ‘Convert Categories to Tags’ feature in WordPress. Hadn’t used it on hosted blogs earlier, so I was shocked to see that it DID copy all categories into tags – but at the same time, it deleted those categories too. Hell, those posts didn’t even come under the default category, they simply went off the radar. Since it was a time when I was doing multiple changes to my blog per day, I hadn’t bothered to take a minute-by-minute database backup. I had nothing to fall back to. No problem, I thought, other people must have faced the same problem, and there must be SOME script / plugins out there to handle the conversion of tags back into categories. Surprisingly, there weren’t any.

The Solution
Spent the rest of the day trying to figure out the WordPress database, and a few trials (and a some backup restores later), I hit upon a way to do it.

  1. If there are any existing categories which are named exactly the same as any tags, delete them from Manage > Categories on your WordPress dashboard. The only reason I say this is because it’ll save you a lot of headache while editing the database.
  2. First thing, whenever you do ANY sort of mucking about with databases – back it up! On hosts like GoDaddy, you’ll have an option in your hosting manager to create an SQL dump; otherwise, open up your database manager (on the server) and choose the Export option.
  3. Login to your database manager – which would be phpMyAdmin generally. You’ll need to use the username and password you created initially for this. Generally, the username could be the same as your database name. In case it isn’t, you should’ve written it down, dum dum. :p
  4. Open the database related to WordPress and open the table wp_terms_taxonomy. Check phpMyAdmin’s documentation if you don’t know how. Explaining that is not my job.
  5. Check the box next to any fields which are marked as post_tags, leaving the category and link_category fields as it is. Then, click on the Edit icon at the bottom of the table.
  6. Replace post_tags with the text category in the Edit page. No other change is necessary. Submit the form.
  7. If you run into a MySQL error, of the sort – ‘xx-category already exists’, where ‘xx’ is a number – that has happened because a category and a tag have the same name, and therefore that particular post_tag can’t be renamed to its equivalent category field.

That’s that! Login back to your WordPress dashboard, and you’ll find that all categories have been restored using the tags which were created. Personally, on WordPress, I feel that the categories feature is better than tagging – at least presents a list to choose from! Tagging, on the other hand, doesn’t have auto-complete (by default, at least) on WordPress.

And frankly, WordPress’ categories to tags converter sucks – first, erasing categories, and then not even warning the user before doing so.

25 replies on “Converting Tags To Categories In WordPress”

I agree that it sucks. No warning and then bam, one of your best navigational features is destroyed (categories listed at the bottoms of posts).

I just accidentally did this too, but I’m currently on a blog…

I really appreciate that you took the time to post this fix.

Unfortunately it wont help me. I’m trying to find a script or something to hack the export file ( WordPress eXtended RSS or WXR), so I can re-import it and have my categories back. Let me know if you know of anything.

Really I think WP has made a few unwise decisions recently. The first was to insert automatically generated tags (based on users’ cat names) at the bottoms of our posts so when someone clicks on them, rather than seeing the posts by that blogger under that tag or category, the reader is taken to WP’s blog-surfing, technorati-like blog search results. I’m so disappointed, but I can’t move to my own hosting a for a month or two. In the meantime, My blog’s navigation is destroyed. So sad.

Thanks a gain for the post.

@Andrew: Thanks Andrew! Glad to know my post has helped. Yes, it would be much easier for you to make this change from the database. I’ll make a new post on this one, and link it here, on what you could possibly do. I don’t know whether it will work, let’s hope it does.

Actually, what I did is, I had a friend let me do a temp WP install on his server, I exported, imported to the tenp install, and he performed the MySQL hack… Then I deleted all my posts and imported an export from the tem install to my blog. Nearly perfect. He got a lot of errors along the way, and I think your DB hack might be out of date with WP2.5x… Not sure about that but he did say that some of the tables were different or non-existent compared to the above…

Either way, you helped me… I wish there was a web-app to do this… A scripts that re-writes the wordpress export XML file… that’d be sweet.

Anyway… thanks again and cheers. I’m in Sebastopol CA. Drop me a line if you’re ever in the area. I owe you hi-fives.


Oh… and now I have this weird bug:
I have a few cats that show up in my sidebar, but don’t show up in my manage > Categories section of my dashboard. When I click on those cats, the URLs have extra slashes in them. I assumed this was a syntax problem with the new data added to the DB during the fix on the temp wp site, but my friend doesn’t think so. The URLs look like:

If you want to see, look at the “paintings” cat on my blog… I’d be glad to give you my login info too, so you can see the dashboard side of this bug, but thru email or aim or something. Let me know.


@Andrew: I posted a new post on my blog, and emailed the link to the email address listed on your blog. That method uses editing XML files, but I haven’t tried it out, although it SHOULD work. Check it out at

Also, it’s weird that when your friend exported using the database it added extra characters. That shouldn’t be happening. Which database type (MySQL or PostgreSQL) and what version number was he using? Yes, it’s possible that my method may not be working for WP 2.5, the database structure could have changed. I’ll have a look at it and see if that’s the case.

@Andrew (and everyone else): The method works fine even with WordPress 2.5.x, there is NO CHANGE in the database structure. However, I got where you might have entered a problem. When you first login to the database, and then click on the wp_terms_taxonomy link, your MySQL database can be configured to show the ‘Structure’ tab instead of ‘Browse’ tab by default. Click on the ‘Browse’ tab at the top, and you will get the actual database entries, which is what I mentioned here. Otherwise, I don’t see any difference between 2.5.x and 2.3.3.

I’m glad I saw your post, it saved me from making a nasty mistake

(I’ve supplied the URL for what I’m thinking of doing in the ‘website’ box)

@Mark: Phew, lucky escape for you. :) Yes, most people (quite correctly) believe that WordPress won’t delete the categories, and get into a lot of trouble when they go ahead and do it.

Thanks Ankur.
A client wants to turn categories into tags and visa-versa (for reasons too complicated to go into here) and I suspected it was as simple as swapping the taxonomy values in wp_term_taxonomy… It’s good to have confirmation;-) I’ll be doing this in WP 2.6 but I’ve an idea things might be different in 2.7

As far as I remember, I wrote this post when I was using 2.6. Even if it wasn’t 2.6 (I doubt that), basic things such as wp_term_taxonomy aren’t changed between versions. If it’s a mission-critical thing then take a backup of the database you’re working on, then make the changes. If anything goes wrong, then you can use phpMyAdmin to import the backup and restore everything to its original state. WP-DB-Backup is an excellent plugin for this task.

Yeap, it sucks big time. FORTUNATELY for me I set it up on a brand new blog with no posts – I had just spent about an hour nesting a whole bunch of subcategories and ran it thinking it would “copy” (duh, it *says* “convert” – what was *I* thinking?) the categories to tags. So most fortunately I only lost only about fifteen minutes of work because I could convert them *back* only they are no longer nested (which is the 15 minutes I’ll have to spend re-doing them). If it had been one of my older blogs I would have definitely been pissed off.

My advice for anyone not SQL geeky enough to deal directly with databases – get the WP-DBManager plugin. It’s the lazy man’s Swiss Army Knife of WP Database Management.

You can run the SQL:

UPDATE `yourdbname`.`wp_term_taxonomy` SET `taxonomy` = ‘category’ WHERE `wp_term_taxonomy`.`taxonomy` =’post_tag’

Please not that you will have to reconstruct the hierarchy;

“Generally, the username could be the same as your database name. In case it isn’t, you should’ve written it down, dum dum. :p”

IF it isn’t the same as the username then install the WP Plugin WP-DBManager (used to backup and manage WP Databases) and it will tell you the name of the Database.

Chief Dum Dum

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.