Add a Tag to each Post in a Category

A WP user had a large number of posts in a certain category.  They wanted to add a tag to each post in that category.  Here is the procedure to do that with SQL commands. 

1. Create the new tag.

2. Get the new tag’s term_taxonomy_id (assume it is 4 for this example)

3. Get the category’s term_taxonomy_id (assume it is 55 for this example)

4. Create a table from all rows in wp_term_relationships where
the term_taxonomy_id is 55.

CREATE TABLE temp
SELECT * FROM wp_term_relationships
WHERE term_taxonomy_id = 55

5. Insert a term_relationships row for each row in the temp table.

INSERT IGNORE INTO wp_term_relationships
SELECT temp.object_id, 4, 0
FROM temp
LEFT JOIN wp_posts p ON (p.ID = temp.object_id)
WHERE p.post_type = ‘post’
AND p.post_status = ‘publish’

Leave a Reply

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

*