Symptom. A template prints a category that was removed an hour ago. A REST response omits a tag that the editor just added. You check the database and it agrees with the edit, not with the page. The instinct is to blame get_the_terms() or the query. The more useful diagnosis is that one of two distinct caches is stale, and the repair depends on which one.
This is a cache-invalidation failure, not a query bug. get_the_terms() is doing exactly what its source says it does. The question is who wrote the term relationship and whether that write path told the cache.
Trace: what get_the_terms() actually reads
The function is short enough to read in full. The relevant branch, per the WordPress developer reference:
$terms = get_object_term_cache( $post->ID, $taxonomy );
if ( false === $terms ) {
$terms = wp_get_object_terms( $post->ID, $taxonomy );
if ( ! is_wp_error( $terms ) ) {
$term_ids = wp_list_pluck( $terms, 'term_id' );
wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
}
}
Two facts follow directly. First, the database is consulted only when get_object_term_cache() returns false. Second, on a miss the function stores the post’s term IDs in the {$taxonomy}_relationships cache group, keyed by post ID. A user-contributed note on the same reference page puts it plainly: the results are cached, so the function will not hit the database every time it is called.
Contrast that with wp_get_object_terms(), which the reference page notes is not cached and will result in a database call every time it is invoked. That is the escape hatch when you need ground truth, and the reason it is a poor default inside a loop.
Two caches, two failure modes
The single most useful distinction in this class of bug is between the relationship cache and the term object cache. They go stale for different reasons and are repaired by different calls.
The relationship cache answers: which term IDs belong to this post in this taxonomy? It lives in the {$taxonomy}_relationships group. If it is stale, the term list is wrong — a removed term still appears, or a newly assigned one is missing.
The term object cache answers: what are the fields of term 42 — its name, slug, description, parent? If it is stale, the list is correct but a field is old. The 2013 Trac report on this exact cache describes the second case: edit a tag’s description, call get_the_terms(), and the old description comes back. The report names get_the_terms() as an affected caller and notes that reproducing it requires some form of persistent caching.
That last detail matters for triage. The default WordPress object cache is non-persistent and stores objects only for the life of the current request, as described in this overview of the object cache. A persistent object cache, by contrast, saves cached objects across requests. So a stale-cache bug that is invisible on a stock install can appear the day a persistent backend is introduced. Treat it as a deployment-time regression, not necessarily a code change.
Root cause: writes that bypass the invalidation path
Core’s documented write path does invalidate. wp_set_object_terms() ends with:
wp_cache_delete( $object_id, $taxonomy . '_relationships' );
wp_cache_set_terms_last_changed();
That is the contract: assign terms through this function and the relationship key for that post and taxonomy is deleted, so the next get_the_terms() call misses and re-queries. The reference page also documents the return value as term taxonomy IDs, not term IDs — worth remembering when you are debugging why a comparison fails.
The failure mode is code that writes term relationships without going through that function. An import script that inserts rows into term_relationships directly, a migration that copies taxonomy data with custom SQL, a bulk operation that updates the table and then renders a summary in the same request — none of these trigger the wp_cache_delete() above. The relationship cache keeps serving the pre-write term IDs until something else invalidates it. The database and the page disagree, and the page is the one that is wrong.
There is a second, subtler bypass: the $_wp_suspend_cache_invalidation global. clean_object_term_cache() checks it and returns early when it is set:
global $_wp_suspend_cache_invalidation;
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
return;
}
If any code in the request has set that global — core does during some bulk operations — your explicit invalidation call becomes a no-op. That is by design, but it means a repair that works in isolation can silently fail inside a larger operation.
Fix: invalidate the right cache with the right call
For the relationship cache, the documented repair is clean_object_term_cache(). Per its reference page, it removes the taxonomy relationship to terms from the cache for the given object IDs and object type, and internally deletes the relationship keys per taxonomy:
wp_cache_delete_multiple( $object_ids, "{$taxonomy}_relationships" );
The object type argument is the post type name, not the taxonomy. Passing the wrong type means get_object_taxonomies() returns the wrong set and the keys you care about are never deleted. A correct call after a direct write looks like this:
// After writing term relationships outside wp_set_object_terms().
clean_object_term_cache( $post_id, get_post_type( $post_id ) );
For a batch, pass an array of IDs. The function accepts a single ID or a list.
For the term object cache — the case where the list is right but a field is old — the repair is different. You need to invalidate the term itself, not the post’s relationship key. Core’s term update paths handle this; custom code that updates term fields with direct SQL does not. If you are writing term fields yourself, invalidate the term cache for the affected term IDs after the write, and do not assume clean_object_term_cache() covers you. It deletes relationship keys, not term objects.
Verification: prove which cache is stale before you fix it
Do not guess. Read the cache directly. wp_cache_get() takes a key, a group, and an optional $found flag passed by reference, which disambiguates a stored false from a miss — the reference page is explicit about this and it is the difference between a real diagnosis and a coin flip.
$found = null;
$cached = wp_cache_get( $post_id, 'category_relationships', false, $found );
var_dump( $found, $cached );
If $found is true and $cached is an array of term IDs, compare it against the database:
SELECT tt.term_id
FROM wp_term_relationships AS tr
JOIN wp_term_taxonomy AS tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
WHERE tr.object_id = 123 AND tt.taxonomy = 'category';
If the cached IDs and the SQL result differ, the relationship cache is stale. If they match but a field such as description is old, the term object cache is stale. The two diagnoses lead to different repairs, which is the whole point of separating them.
For a quick check without writing PHP, WP-CLI can confirm the database side:
wp post term list 123 category --fields=term_id,name,slug
If WP-CLI shows the correct terms and the rendered page does not, you have localized the problem to the cache layer rather than the data layer.
Audit checklist for custom term-assignment code
Walk every code path that assigns terms and answer two questions.
Does the write go through wp_set_object_terms()? If yes, the relationship key is deleted for you. If no — direct $wpdb inserts, custom SQL, a migration tool — you must call clean_object_term_cache() with the correct post type afterward, and you must confirm $_wp_suspend_cache_invalidation is not set at that moment.
Does any code read get_the_terms() earlier in the same request than the write? If a template or REST response fires before the assignment completes, it populates the relationship cache with the old IDs. The later write deletes the key, but if the read happens again after the delete and before the database reflects the write — or if the write bypassed invalidation entirely — you are back to stale data. Ordering matters as much as the function call.
One more: if you are debugging a term-field problem rather than a term-list problem, the relationship audit will not help. Check whether the term itself was updated through a path that invalidates the term object cache, and treat the 2013 report as a description of the failure shape rather than a claim about current core behavior. The retrieved report shows the ticket filed as a defect with no resolution recorded; whether the gap persists in your version is something to verify against your own install, not assume from a decade-old mailing-list post.
FAQ
Why does this only happen on some sites? Because the default object cache is per-request. A stale entry that would be discarded at the end of the request becomes durable once a persistent backend is in place. The code did not change; the cache lifetime did.
Should I just call wp_cache_flush()? It will make the symptom disappear and tell you nothing. It also discards every other cached object on the site, which on a busy install is its own incident. Invalidate the specific key or object you wrote.
Is get_the_terms() broken? No. The source shows it reading the cache first and falling back to the database on a miss, exactly as documented. The bug is in the write path that failed to invalidate, or in the read ordering that populated the cache too early.
What about the block editor and REST term assignment? The REST posts controller calls wp_set_object_terms() to handle terms, so the documented invalidation runs on that path. If you see stale terms after a REST update, look at what else in the request read the relationship cache before the controller wrote, or at a custom endpoint that writes terms its own way.