Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Wordpress

WordPress: Fix Taxonomy Term Counts Drifting After a Bulk Import

- 02.12.26 - ErcanOPAK

📝 WordPress: Fix Taxonomy Term Counts Drifting After a Bulk Import

WordPress keeps a cached count column on each row in wp_term_taxonomy rather than counting wp_term_relationships rows live on every page load, and that cached count is only recalculated by the normal save flow — the hooks that fire around wp_set_object_terms() during a single post save in the admin. A bulk importer that inserts post rows and term relationship rows directly via $wpdb, or through wp_insert_post() called with term assignment skipped for speed and term relationships added afterward in a separate raw query, never triggers that recount path at all, so every affected term’s displayed count on the category/tag archive pages, in widgets, and in the admin term list keeps showing whatever it was before the import — sometimes zero — no matter how many posts actually carry that term now.

🔎 The Problem

-- What the bulk importer actually ran (fast, but skips recount hooks):
INSERT INTO wp_term_relationships (object_id, term_taxonomy_id)
VALUES (4821, 17), (4822, 17), (4823, 17), /* ...1200 more rows... */;

-- Reality after the import:
SELECT COUNT(*) FROM wp_term_relationships WHERE term_taxonomy_id = 17;
-- 1,203

-- What WordPress is still showing everywhere (archive pages, widgets, admin):
SELECT count FROM wp_term_taxonomy WHERE term_taxonomy_id = 17;
-- 3   <- stale cached value from before the import ever ran

✅ Fix: Force a Real Recount After Any Direct-SQL Import

  • Call WordPress’s own wp_update_term_count_now( $term_taxonomy_ids, $taxonomy ) once, right after the import finishes, passing every affected term_taxonomy_id — it runs the exact recount query the normal save flow would have triggered, just all at once instead of per post.
  • For a very large import, batch that recount call in chunks of a few hundred term IDs per request instead of one call covering tens of thousands of terms, since the recount itself runs a COUNT() query per term and a single giant batch can itself time out on a slow host.
  • Add a WP-CLI step to the import script — wp term recount <taxonomy> covers every term in that taxonomy in one command and is the simplest fix when the importer doesn’t already track which specific term IDs it touched.

🚨 Why This Is Dangerous

  • A category archive page or a “posts in this category” widget that silently under-reports how much content actually exists there looks to an editor like the import failed entirely, sending them hunting for missing posts that are really just sitting there uncounted, not unindexed.

WordPress’s term counts are a cache of a count, not the count itself — and nothing refreshes a cache that the code path updating it was never asked to touch.

— WordPress Admin

Related posts:

WordPress: Customize Login Page for Client Branding

WordPress: Customize Login Page for Client Branding

WordPress: Use Featured Images for Post Thumbnails

WordPress: Use Featured Images for Post Thumbnails

WordPress: Fix the Media Library Showing Duplicate Entries for One Image After a Bulk Import Because...

WordPress: Fix the Media Library Showing Duplicate Entries for One Image After a Bulk Import Because...

Views: 0
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Asp.Net Core: Fix Kestrel Capping Concurrent HTTP/2 Streams
C#: Fix a Generic Math Operator Not Resolving for a Custom Type

Leave a Reply Cancel reply

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

Most Read

  1. 01How to make theater mode the default for Youtube 1,080 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 997 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 996 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 869 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 672 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 631 views
  8. 08Average of all values in a column that are not zero in SQL 585 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 562 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com