#45232 closed defect (bug) (invalid)
get_object_term_cache() returns an empty array
Reported by: | sebastienserre | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 5.0 |
Component: | Taxonomy | Keywords: | |
Focuses: | Cc: |
Description
Hello
I'm in a case where get_object_term_cache() in get_the_terms() returns an empty array evenif I have term (as post_format) for the posts.
in
$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' ); } }
checking if $terms is also empty solves the problem.
if you think it's a "bug" I'll create the patch.
Attachments (1)
Change History (7)
#3
@
6 years ago
Hello,
Unfortunately I don't know how to reproduce.
A new customer with a WP 4.3.6 asked me to upgrade to the latest (4.9.8)
Blog Post use post-format and they're checked. And in db the terms are registred. I'll try clean_term_cache().
Many thanks @boonebgorges for the clear answer
Note: See
TracTickets for help on using
tickets.
Hi @sebastienserre - Thanks for the ticket and for the patch.
WP intentionally caches empty arrays in cases where an object doesn't have any terms in a given taxonomy. This helps us to avoid excess database queries when, for example, you're fetching the
post_tag
terms for post123
, but123
doesn't have any terms frompost_tag
. Your suggested change will break this behavior, so that term queries that result in empty arrays will never be cached.If
get_term_object_cache()
is returning an empty array, it suggests that there's a problem further up the stack. The most likely problem is that the{$taxonomy}_relationships
cache is not being properly invalidated somewhere in your setup.Are you able to reproduce your bug on a stock installation of WordPress, with no additional plugins and a clean database? Does it occur only with a persistent caching backend (Memcached, etc)? Can you provide steps to reproduce?