Google PlusFacebookTwitter

Converting Tags To Categories In WordPress

By on Mar 25, 2008 in Personal, Tech Takes | 23 comments

Share On GoogleShare On FacebookShare On Twitter

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.



23 Comments

  1. 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.

  2. Ankur

    May 16, 2008

    Post a Reply

    @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.

  3. 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.

  4. 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.

  5. Ankur

    May 16, 2008

    Post a Reply

    @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 http://www.ankurb.info/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.

  6. Ankur

    May 17, 2008

    Post a Reply

    @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.

  7. Murk

    June 17, 2008

    Post a Reply

    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)

  8. Ankur

    June 17, 2008

    Post a Reply

    @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.

  9. Shankar

    January 4, 2009

    Post a Reply

    thanks mate! life-saver post…absolutely moronic of wordpress to include such a converter that does this stuff without warning…

  10. Zygotti

    May 3, 2009

    Post a Reply

    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

    • Ankur

      May 3, 2009

      Post a Reply

      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.

    • Suthnautr

      December 30, 2010

      Post a Reply

      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.

      • Ankur Banerjee

        December 30, 2010

        Nice tip, hadn’t heard of that plugin.

  11. Alcides Soares

    March 29, 2010

    Post a Reply

    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;

  12. Dave

    August 16, 2010

    Post a Reply

    Thanks Ankur, I knew there had to be an easy way to convert some wordpress tags to categories.

    Much Appreciated
    Dave

  13. Suthnautr

    December 30, 2010

    Post a Reply

    “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

Trackbacks/Pingbacks

  1. WordPress Hack: Convert Tags To Categories « Andrew A. Peterson and Ramping Up - [...] ankurb.info, or “Needlessly Messianic” has a solution for you.  Thanks, philanthropic wordpress community member!   [...]
  2. Comments « Murky Blog - [...] The Cat2Tags feature does remove category info. Glad I saw this post. I’d want to make tags of categories…

Submit a Comment

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