Add Taxonomy to Wordpress posts in bulk if post title contains text

1.7k Views Asked by At

I would like to add the Taxonomy term "Michael Jordan" to every published post that has the text "Michael Jordan" somewhere in its post title.

I assume you should be able to do this via MYSQL injections using PHPMYADMIN with the right query. However Wordpess's database tables for taxonomies and terms is a bit complicated for my level of MYSQL.

I can select all the posts from the wp_posts table with the term "Michael Jordan" using.

SELECT * FROM `wp_posts` WHERE `post_title` LIKE '%Michael Jordan%'; 

I assume I would use the Post IDs from this result, to identify which posts to add the Taxonomy term.

Thank you for help with forming the right query.

1

There are 1 best solutions below

11
On

First, take a backup of your database before proceeding.

  1. Find the term_id for your term "Michael Jordan"

select * from wp_terms where name="Michael Jordan" , which should give you similar output

| term_id | name           | slug           | term_group |
+---------+----------------+----------------+------------+
|      12 | Michael Jordan | michael-jordan |          0 |

Note the term_id (yours will be a different number)

  1. And then the following query

INSERT INTO wp_term_relationships SELECT id,term_id_noted_on_step_1,0 FROM wp_posts WHERE post_title LIKE '%Michael Jordan%'

Just in case you'd like to go the WordPress route, check wp_set_object_terms