Turning WordPress Categories Into Tags

I recently updated this WordPress install from 2.2.something to 2.5 and noticed that it now supports tags instead of just categories. I had been using categories as tags, but I’d rather built-in tags than fake it anymore. I didn’t want to manually retag a couple hundred posts with ~200 tags, so I wrote some SQL to do it for me. If you’re in the same boat

Warning: This could break and completely mess up your categories and tags. I’m only promising this worked for me on my install for now (who knows, maybe I updated things wrong and huge problems will occur a month from now). Use caution. I take no responsibility.

 
# create tags for every category
INSERT INTO wp_term_taxonomy (term_id, taxonomy)
    SELECT term_id, "post_tag"
    FROM wp_term_taxonomy
    WHERE taxonomy = 'category';
 
# copy category-post relationships to tag-post relationships
INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) 
    SELECT 
        object_id, 
        (SELECT i.term_taxonomy_id
         FROM wp_term_taxonomy i
         WHERE
             taxonomy = 'post_tag' AND 
             i.term_id = wp_term_taxonomy.term_id)
    FROM wp_term_relationships 
    JOIN wp_term_taxonomy ON 
        wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id 
    WHERE taxonomy = 'category';
 
# update tag count cache
UPDATE wp_term_taxonomy 
    SET count = (SELECT count(*)
        FROM wp_term_relationships
        WHERE wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id);
 
# remove all categories and category-post relationships
DELETE FROM wp_term_taxonomy WHERE taxonomy = 'category';
DELETE FROM wp_term_relationships 
    WHERE term_taxonomy_id NOT IN (SELECT term_taxonomy_id FROM wp_term_taxonomy);

The INSERT INTO ... SELECT was a fun little query to write with its subselect. After running this, I just had to create and apply categories to my posts (I’m going with Biz, Code, Games, and Life for now) and update my templates.

Want more? I'm not as good at forgetting to update @pushcx on Twitter.