WordPress Tip: Adding All Posts to a Tag

In moving my blog across from WordPress.com, I decided I wanted to review all of the posts that I imported. To track which ones I had done and which ones I had not, I created a tag to apply to all of the posts. Then, the challenge was to apply that tag to all of the posts. I couldn’t find anything on the front end, so I decided to try the database.

Here are my explorations in applying that tag:

First, here is the SQL that displays my new tag.  The slug (URL-friendly version of the name) of my new tag is “tomove”.  So, this SQL displays the ids for that slug:

select a.term_id, a.name, a.slug, b.term_taxonomy_id
from wp_terms a, wp_term_taxonomy b
where a.term_id = b.term_id
and b.taxonomy = 'post_tag'
and a.slug = 'tomove'
Then, I modified that to insert a row for each published post:
insert into wp_term_relationships (object_id, term_taxonomy_id, term_order)
select c.id object_id,  b.term_taxonomy_id term_taxonomy_id, 0 term_order
from wp_terms a, wp_term_taxonomy b, wp_posts c
where a.term_id = b.term_id
and b.taxonomy = 'post_tag'
and a.slug = 'tomove'
and c.post_type = 'post'
and c.post_status = 'publish'
And, then, I checked from the Dashboard, and my posts had the tag!


Leave a Comment

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