Copy Categories to Tags – The Plugin

by Josh Kenzer on March 7th, 2008

Part 2 of the previous Wordpress Tag Post

In my continuing effort to duplicate existing categories associated with posts as tags, I offer up this plugin. Remember, my goal is to phase categories out in favor of tags. So I don’t want to do just an abrupt change over switching my categories to tags. Instead, I want to duplicate them, and transition away from categories. First in the templates, then cleaned out of the database.

DOWNLOAD here

Here’s the process:
For each post, get a list of the categories. For each category, determine if the category (term_id), exists in the wp_term_taxonomy table. If it does, it will have the taxonomy of “post_tag”

Does the category already exist as a post_tag?

If Yes
Retrieve the term_taxonomy_id for that term, and insert it into wp_term_relationships. Also, update the count in the wp_term_taxonomy table for that post_tag – important for tag clouding.

No
Create a new record in the wp_term_taxonomy table referencing the same term_id but with a taxonomy of post_tag. Set the count to 1 on insert.

In either case:
Retrieve back the term_taxonomy_id, and insert that into wp_term_relationships for the post.

  • Hi! Thanks for the plugin! I was looking for the same thing and this worked great after a few tweaks. With 600+ posts I wasn't looking forward to moving each over one by one but, like you, I wanted to still keep many of the categories in tact for link purposes so the converter wouldn't cut it for me.

    My only problem was that I don't have my blog in the same directory as the index file so I just had to go in and add a /blog in front of the link in the php file and all worked well!

    Thanks again!
  • skiaddict
    Doesn't seem to work for posts that have multiple categories - appreciate the effort though!
  • Works fine!
  • Josh Kenzer
    I've made both the changes that Herr and charlie suggested. I've uploaded the new zip file.

    I also got the permissions to post it to extend.wordpress.org, so I will be doing that shortly.
  • Hi there, thanks for the very useful plugin - I ran into the same problem as Herr Rau, above, but the problem was easily fixed. It should be just a matter of appending the bloginfo('url') function ahead of the form action and most users shouldn't have a problem. Perhaps you could implement both that as well as the db prefix change for a future release.

    I used to use UTW's feature of automatically copying categories to tags as I selected them, but since version 2.3, I'd been neglectful in my tagging, so your plugin was great for getting me back on track. Thanks.
  • Thanks for the plugin, I've been looking for a way to switch to tags while keeping most categories, for similar reasons to yours.

    A slight suggestion: In your plugin, you have hardcoded the database table prefix as "wp_" - my prefix is different, so I used the $wpdb->prefix variable instead to make it work.

    (Actually, I did nothing of the sort. I just hardcoded *my* prefix, but really should have used the variable.)

    Also, my installation is not in the root but in a folder, so to get the plugin to work I had to change the form action from:
    "/wp-admin/plugins.php?page=copy_..."
    to:
    "plugins.php?page=copy_..."
    to get it to work.

    But now it works beautifully, thanks.
  • Josh Kenzer
    Yes, for the current WordPress version, the plugin is called Simple Tags (http://wordpress.org/extend/plugins/simple-tags) and it's pretty awesome. However, in the next major release of WordPress, this functionality is built in.
  • Is there a plugin that lets you select tags from a list in wordpress the way you can select categories? Cause w/o that tags are kinda lame cause you end up duplicating tags i.e. "bike" and "bikes" etc...
blog comments powered by Disqus