Update Custom Taxonomy Term Counts in Wordpress

This post is basically just a reminder for myself if I need to do this again. I’ve been using WP All Import to imports posts into the Shirt List and I think because I imported them as drafts they taxonomy count didn’t update. That means that when I used code to display the number of t-shirts in a custom taxonomy, it often under-represented the actual figure. I used the following code to run a MySql query:

UPDATE wp_term_taxonomy SET count = (
SELECT COUNT(*) FROM wp_term_relationships rel 
LEFT JOIN wp_posts po ON (po.ID = rel.object_id) 
WHERE 
rel.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id 
AND 
wp_term_taxonomy.taxonomy NOT IN ('link_category')
AND 
po.post_status IN ('publish', 'future')
)

I found the code on Stack Overflow.

Caution: Use the code at your own risk. Make a site backup before you run any queries.

2 Comments

Leave a Reply

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