19

How to update WordPress taxonomies(categories/tags)' count field after bulk import/delete?

Related questions:

WordPress › Support » Fix comment and category counts after import http://wordpress.org/support/topic/fix-comment-and-category-counts-after-import

4 Answers 4

73

This SQL helps:

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')
)
4
  • Works perfectly and very fast Nov 23, 2014 at 15:16
  • 1
    I just had to add 'inherit' to last post_status array to correctly count terms for attachments Jun 13, 2018 at 2:54
  • Didn't work for me, but I'm using taxonomy with media uploads. Not sure if that matters. Jan 9, 2020 at 3:10
  • This worked for me for WooCommerce attributes but not for product categories. The WP-CLI answer helped with them.
    – Melebius
    Sep 16, 2021 at 14:17
20

A more WordPress-compliant way would be using wp_update_term_count_now (https://developer.wordpress.org/reference/functions/get_terms/)

Example:

$update_taxonomy = 'my_taxonomy';
$get_terms_args = array(
        'taxonomy' => $update_taxonomy,
        'fields' => 'ids',
        'hide_empty' => false,
        );

$update_terms = get_terms($get_terms_args);
wp_update_term_count_now($update_terms, $update_taxonomy);
9
  • I simply can't get this to work. "Argument #2 should be an array" Oct 16, 2018 at 10:58
  • Function reference says it has to be a string: developer.wordpress.org/reference/functions/… wp_update_term_count_now( array $terms, string $taxonomy ) Oct 17, 2018 at 14:32
  • But I literally used your code above, swapping out the taxonomy name. Oct 17, 2018 at 19:02
  • @RobertAndrews: What happens, if you use the code above like it is, without swapping out the taxonomy name? And you could put an "echo" before "wp_update_term_count_now". Nothing is changing in your system, but the code should work without an error and "1" should be echoed. Oct 18, 2018 at 21:30
  • @Martin-from-WP-Stars-com Leaving 'my_taxonomy' and prefixing wp_update_term_count_now with echo doesn't solve the "Argument #2 should be an array" problem. So, the wp_update_term_count_now line also throws it's "trying to get property of a non-object". wp_update_term_count_now expects your $update_taxonomy to be an array. Oct 19, 2018 at 9:35
7

The answer by @kaorukobo works well. However, I needed a bit more customization to automatically update all terms for a CPT... specifically a WooCommerce product CPT with several custom taxonomies...

The following updated all the taxonomies for the product CPT.

UPDATE wp_term_taxonomy tt
    SET count =
    (SELECT count(p.ID) FROM  wp_term_relationships tr
    LEFT JOIN wp_posts p
    ON (p.ID = tr.object_id AND p.post_type = 'product' AND p.post_status = 'publish')
    WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
)
3
  • This worked perfectly for me on my Woocommerce website. I was manually setting categories to posts. This nice piece of code worked nicely after I ran after these inserts. Thanks (and thanks @kaorukobo)
    – Sim2K
    Sep 17, 2017 at 1:47
  • This works also perfect for me after importing some product properties (terms) without WordPress functions. Thanks!
    – NVO
    May 9, 2018 at 14:36
  • This has not corrected WooCommerce product categories for me. The WP-CLI answer helped with them.
    – Melebius
    Sep 16, 2021 at 14:18
6

In my opinion the best way to do this is by using WP-CLI. There's a command for that:

$ wp term recount category

Find the documentation here: https://developer.wordpress.org/cli/commands/term/recount/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.