Opened 14 years ago
Closed 12 years ago
#16505 closed defect (bug) (fixed)
WP should cache that a post has no terms
Reported by: | nacin | Owned by: | nacin |
---|---|---|---|
Milestone: | 3.5 | Priority: | normal |
Severity: | normal | Version: | 3.0 |
Component: | Taxonomy | Keywords: | has-patch |
Focuses: | Cc: |
Description
Despite the initial fetch of all terms for all queried objects, any object that has no terms in any taxonomy will not be cached. This causes an additional query for each of these objects, come the time a template tag needs to pull taxonomy data from cache.
Each additional template tag (I'm referring to has_term() and friends) will continue to generate the query on each call. On stock Twenty Ten, this can double the number of queries per pageload due to the in_category calls in loop.php. It seems get_post_format() is spared, but I didn't dig as to why. The difference is is_object_in_term() in in_category() versus get_the_terms() in get_post_format().
While this won't (normally) happen for posts due to the default category, this can happen for custom post types pretty easily. (I say 'normally,' because that's exactly what I was just tracking down for the past 45 minutes.)
The fix here is to get WP, probably in update_object_term_cache(), to discern the difference between having not yet fetched terms for an object, and having already tried to fetch terms for an object but none existed.
Just checked, and get_object_term_cache will return an empty array if there are no terms, so an empty result is being cached, it just isn't being parsed correctly in all instances. See #18392.