Make WordPress Core

Opened 9 years ago

Closed 8 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:


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.

Attachments (1)

check-false-get-object-terms.diff (1.2 KB) - added by wonderboymusic 8 years ago.

Download all attachments as: .zip

Change History (6)

#1 @scribu
9 years ago

  • Cc scribu added

#2 @wpdavis
9 years ago

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.

#3 @wonderboymusic
8 years ago

  • Keywords has-patch added; needs-patch removed
  • Milestone changed from Future Release to 3.5

There were 3 places that needed false ===. Patch attached.

#4 @nacin
8 years ago

This looks solid.

#5 @nacin
8 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In 22433:

Only call wp_get_object_terms() if get_object_term_cache() says there is no cache (false), which is different than no terms. props wonderboymusic, fixes #16505.

Note: See TracTickets for help on using tickets.