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”

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

Word.

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:
name.wordpress.com/category//categoryname/

If you want to see, look at the “paintings” cat on my wp.com 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.

Peace.

@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 https://ankurb.net/2008/05/16/converting-tags-to-categories-in-wordpress-using-the-xml-file/

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.

Leave a Reply

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