Fixes incorrect WooCommerce product category counts when moving subcategories between parents or deleting categories. Addresses WooCommerce issue #25491 — open since January 2020.
When you move a subcategory from one parent to another in WooCommerce, the product counts on both parents become wrong:
- Old parent keeps the inflated count (still includes the moved subcategory's products)
- New parent keeps the deflated count (doesn't include the moved subcategory's products)
The same happens when deleting a category — the former parent's count goes stale.
- Create Category A with 5 products
- Create Category B (empty)
- Create Subcategory C under Category A with 3 products
- Category A shows count = 8 (5 own + 3 from C) ✓
- Move Subcategory C from Category A to Category B
- Category A still shows 8 ✗ — should be 5
- Category B still shows 0 ✗ — should be 3
This affects the product category widget, shop page counts, layered navigation, and any code relying on get_terms() with WooCommerce's term count override.
WooCommerce stores hierarchical product counts in wp_termmeta via _wc_term_recount(). This function recounts a category's products including all descendant categories.
The recount is triggered when products are assigned to or removed from categories (set_object_terms hook). But it is not triggered when:
- A category's parent is changed (
edited_product_cat) - A category is deleted (
delete_product_cat)
So moving or deleting categories leaves the parent counts stale until a product is added or removed from those categories, or a manual recount is run.
Three hooks, one batched recount:
-
edit_product_cat(pre-update) — Snapshots the category's current parent before the edit happens. This captures the "old" parent so we can recount it after the move. -
edited_product_cat(post-update) — Queues the edited category, its new parent chain, and its old parent chain for recount. -
delete_product_cat— Queues the deleted category's former parent chain for recount.
All queued term IDs are batched and recounted once on shutdown, so bulk edits, imports, and REST API batch operations don't trigger redundant queries.
The plugin uses WooCommerce's own _wc_term_recount() function — the same function WooCommerce already uses for product assignment recounts. This ensures the fix respects:
- Hierarchical counting (parent includes children)
- Visibility exclusions (hidden products, out-of-stock if configured)
- Transient cache clearing (
wc_term_counts)
- Download
woocommerce-category-count-fix.php - Upload to
wp-content/plugins/woocommerce-category-count-fix/ - Activate in Plugins > Installed Plugins
- Download
woocommerce-category-count-fix.php - Upload to
wp-content/mu-plugins/ - No activation needed — it loads automatically
wp plugin install https://github.com/renzojohnson/woocommerce-category-count-fix/archive/refs/heads/main.zip --activateIf your category counts are already wrong, run a one-time recount:
WooCommerce > Status > Tools > Recount terms
This recalculates all existing counts. Going forward, the plugin keeps them accurate automatically.
The plugin logs every recount operation to WooCommerce's logger:
[category-count-fix] Category counts recalculated for term_ids: 42, 15, 8
View logs at WooCommerce > Status > Logs and select category-count-fix from the source dropdown.
- WordPress 6.0+
- WooCommerce 7.0+
- PHP 8.1+
No. The plugin only fires when a category is edited or deleted — not on every page load. Recounts are batched per request, so even bulk operations run a single recount query.
Yes. This plugin operates on term metadata, not order storage. It works regardless of whether you use HPOS or the legacy post-based order storage.
Yes. WordPress REST API term updates and deletions flow through the same core functions that fire edited_product_cat and delete_product_cat.
Yes. WP-CLI term commands use the same core APIs and fire the same hooks.
That tool does a one-time full recount. This plugin keeps counts accurate automatically so you don't need to run it manually after every category change.
Yes. Upload the single PHP file to wp-content/mu-plugins/ and it works without activation. This is useful if you want to ensure the fix is always active regardless of plugin management.
- WooCommerce Issue #25491 — "Product count in category is wrong when subcategory is moved"
Renzo Johnson — WordPress & WooCommerce developer.
Questions or feedback? Get in touch or open an issue on GitHub.
GPL-2.0-or-later