#40306 closed defect (bug) (fixed)
Term cache isn't cleared completely when setting and removing object terms
Reported by: | mboynes | Owned by: | boonebgorges |
---|---|---|---|
Milestone: | 4.7.4 | Priority: | normal |
Severity: | normal | Version: | 4.7 |
Component: | Taxonomy | Keywords: | reporter-feedback |
Focuses: | Cc: |
Description
This problem is easiest explained and understood through a unit test; see attached for that plus the patch fixing it.
wp_set_object_terms()
and wp_remove_object_terms()
delete the object cache entry for the object ID in the group {$taxonomy}_relationships
. However, Term_Query
, which ultimately queries for those terms, has its own cache that doesn't get invalidated.Term_Query
's cache key depends on the terms:last_changed
cache entry, so deleting that cache entry will invalidate the cache.
This could/should be a bigger issue than it actually is. The reason it isn't is that the cache gets cleared incidentally via wp_update_term_count_now()
, which performs a pretty greedy cache clear. If one is using an external object cache and disables term counting, the term relationship gets set when the default empty post is created on the "Write" screen and is never erased, and it will forever appear as though a post has no terms (until some other operation invalidates the Term_Query
cache).
Attachments (2)
Change History (7)
#1
@
8 years ago
- Milestone changed from Awaiting Review to 4.7.4
- Version changed from trunk to 4.7
Thanks for the patch, @mboynes. This never came up in the past, because term queries were never affected by term relationships. Since 4.7, when wp_get_object_terms()
was refactored to use WP_Term_Query
, the terms:last_changed
incrementor started applying to object-term queries as well, and this bit of invalidation wasn't caught.
#2
@
8 years ago
- Keywords reporter-feedback added
40306.2.diff reworks the tests so that they're a bit more precise. I'm guessing that your "default post to edit" trick was to work around the "Uncategorized" category. We can eliminate that complication by using a different taxonomy. I've also changed your wp_update_post()
call to wp_set_object_terms()
so that there are fewer variables at play, and added a parallel test for wp_remove_object_terms()
.
Can you give the tests a look to see if they still capture the bug as you've reported it? (The fix itself that you provided seems fine to me.)
Invalidate Term_Query cache on wp_set_object_terms and wp_remove_object_terms