diff --git a/src/wp-includes/class-wp-term.php b/src/wp-includes/class-wp-term.php index 0cefa3097b393..8d0f243f01e98 100644 --- a/src/wp-includes/class-wp-term.php +++ b/src/wp-includes/class-wp-term.php @@ -124,7 +124,7 @@ public static function get_instance( $term_id, $taxonomy = null ) { $_term = wp_cache_get( $term_id, 'terms' ); // If there isn't a cached version, hit the database. - if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) { + if ( ! is_object( $_term ) || ! isset( $_term->taxonomy ) || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) { // Any term found in the cache is not a match, so don't use it. $_term = false; diff --git a/tests/phpunit/tests/term/wpTerm.php b/tests/phpunit/tests/term/wpTerm.php index e640cf8120732..d67f0bd086345 100644 --- a/tests/phpunit/tests/term/wpTerm.php +++ b/tests/phpunit/tests/term/wpTerm.php @@ -89,4 +89,36 @@ public function test_get_instance_should_respect_taxonomy_when_term_id_is_found_ $found = WP_Term::get_instance( self::$term_id, 'wptests_tax2' ); $this->assertFalse( $found ); } + + /** + * @ticket 65915 + * + * @dataProvider data_get_instance_should_ignore_unusable_cached_values + * + * @param mixed $cached_value A cached value that is not a usable term object. + */ + public function test_get_instance_should_ignore_unusable_cached_values( $cached_value ) { + wp_cache_set( self::$term_id, $cached_value, 'terms' ); + + $found = WP_Term::get_instance( self::$term_id ); + + $this->assertInstanceOf( WP_Term::class, $found ); + $this->assertSame( self::$term_id, $found->term_id ); + $this->assertSame( 'wptests_tax', $found->taxonomy ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_get_instance_should_ignore_unusable_cached_values(): array { + return array( + 'an array' => array( array( 'term_id' => 12345 ) ), + 'an integer' => array( 12345 ), + 'a string' => array( 'a term' ), + 'an object with no taxonomy' => array( (object) array( 'term_id' => 12345 ) ), + 'an object with a null taxonomy' => array( (object) array( 'taxonomy' => null ) ), + ); + } }