Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#45232 closed defect (bug) (invalid)

get_object_term_cache() returns an empty array

Reported by: sebastienserre's profile 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)

45232.patch (634 bytes) - added by sebastienserre 6 years ago.

Download all attachments as: .zip

Change History (7)

#1 @sebastienserre
6 years ago

  • Component changed from Post Formats to Taxonomy
  • Keywords has-patch added

#2 @boonebgorges
6 years ago

  • Keywords reporter-feedback added; dev-feedback has-patch removed

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 post 123, but 123 doesn't have any terms from post_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?

#3 @sebastienserre
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

#4 @sebastienserre
6 years ago

  • Resolution set to invalid
  • Status changed from new to closed

It was a not updated plugins problem.
Sorry for conveniences

#5 @boonebgorges
6 years ago

Thanks for reporting back, @sebastienserre !

#6 @ocean90
6 years ago

  • Keywords reporter-feedback removed
  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.