Categories
Personal Reflections Technology

Converting Tags To Categories In WordPress

Update
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 WordPress.com 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.

0 replies on “Converting Tags To Categories In WordPress”

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.

Signed,
Chief Dum Dum

Leave a Reply

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