Using core WordPress functions is ideal when possible for modifying any content. However, when you have hundreds of thousands of posts, updating post content such as taxonomy terms on many items at once can be quite slow. Use SQL statements to quickly update your content only if you know you aren’t going to need any of the associated action/filters hooks for the modifications you are making.
Here’s a MySQL query to apply a specific taxonomy term to all published posts of a specific post type:
INSERT IGNORE INTO wp_term_relationships (object_id, term_taxonomy_id, term_order) SELECT wp_posts.ID as object_id, (SELECT tt.term_taxonomy_id FROM wp_terms t INNER JOIN wp_term_taxonomy tt ON t.term_id = tt.term_id WHERE tt.taxonomy = 'crazy_taxonomy_type' AND t.name = 'Term Name' LIMIT 1) as term_taxonomy_id, 0 as term_order FROM wp_posts WHERE wp_posts.post_type = 'insane_post_type' AND wp_posts.post_status = 'publish'; |
And a query to update taxonomy counts:
UPDATE wp_term_taxonomy tt SET count = (SELECT count(*) FROM wp_term_relationships tr WHERE tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.taxonomy = 'crazy_taxonomy_type'; |