Opened 6 months ago
Closed 6 months ago
#22560 closed defect (bug) (fixed)
Cache invalidation in wp_set_object_terms
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Priority: | normal | Milestone: | 3.5 |
| Component: | Taxonomy | Version: | 3.5 |
| Severity: | normal | Keywords: | has-patch commit dev-reviewed |
| Cc: | batmoo |
Description
In [21981], cache invalidation was added to wp_set_post_terms. Should this not actually go in wp_set_object_terms instead?
The current cache invalidation routine means that a call to wp_set_object_terms and then a subsequent call to get_the_terms will result in stale cache entry returned.
Attachments (2)
Change History (16)
The $taxonomy . '_relationships' cache is set in get_the_terms(), which only works for posts.
So, the cache is cleared in wp_set_post_terms(), which only works for posts.
- Keywords close added
I could see why this can be an issue. Many developers use wp_set_object_terms() for posts, probably not realizing the higher level function exists. I don't think there is any harm in clearing the cache at a lower point in the stack.
Correct me if I'm wrong, but this also isn't a regression? We didn't add caching, just invalidation. Or does the caching apply in new places that could cause issues?
- Milestone set to Awaiting Review
- Status changed from closed to reopened
Replying to nacin:
Correct me if I'm wrong, but this also isn't a regression? We didn't add caching, just invalidation. Or does the caching apply in new places that could cause issues?
Not a regression but currently misleading since the expectation is that it's perfectly okay to use wp_set_object_terms in place of wp_set_post_terms and get the same outcome.
comment:10
nacin — 6 months ago
- Keywords dev-feedback added
I don't think there is any harm in clearing the cache at a lower point in the stack.
I'll re-iterate my earlier statement.
comment:11
ryan — 6 months ago
comment:12
ryan — 6 months ago
- Milestone changed from Awaiting Review to 3.5
comment:13
nacin — 6 months ago
- Keywords has-patch commit dev-reviewed added; dev-feedback removed
If a taxonomy was assigned to completely different objects — say, a post and a user — and the same ID in both had a relationship cache, one would clear the other. This is acceptable, especially because that combination doesn't work anyway, as term relationships don't have a type of object — just an object ID and a tt ID.
Also, there is one early return in wp_set_object_terms() for db errors. We weren't doing a cache delete in wp_set_post_terms() for WP_Error either, so this also seems good.
comment:14
ryan — 6 months ago
- Resolution set to fixed
- Status changed from assigned to closed
In 22878:

Previous discussion that led to the change: http://core.trac.wordpress.org/ticket/21309#comment:72