WP set category to post within SQL

I have a stored procedure and want put posts in the MySQL event to some category (such Archive).

wp_term_relationships (object_id, term_taxonomy_id)
table has link between object_id (post id) and term_taxonomy_id (category id).

To get category by name use query:

SELECT termtaxonomy.term_taxonomy_id 
FROM wp_terms AS terms
JOIN wp_term_taxonomy AS termtaxonomy ON terms.term_id = termtaxonomy.term_id
WHERE terms.name = 'Archive';

To link post with category:

insert into wp_term_relationships (object_id, term_taxonomy_id) values (post_id, category_id);

In addition add update query to update posts count on categories adminpanel:

update wp_term_taxonomy set count=(SELECT count(*) FROM wp_term_relationships WHERE term_taxonomy_id = category_id) where term_taxonomy_id = category_id;

post_id, category_id – external variable values.

Leave a Reply

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

6  +    =  14

Skynin cache:
Array ( [cache mode] => capture [requestKey] => 82c63b2913249d76d9ee781ff17b46a7f1.sc [skycache_disable_capture] => false )