From 1721af83d153f77ee989fc5ae04b112b5d738f07 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 1 Jul 2017 11:45:09 +0000 Subject: [PATCH] Taxonomy: Ignore cached term value when it doesn't match the queried taxonomy. When a cache entry is found that matches the requested `$term_id`, but doesn't match an explicitly provided `$taxonomy`, that cache entry should be ignored. Props GunGeekATX. Fixes #40671. git-svn-id: https://develop.svn.wordpress.org/trunk@40979 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-term.php | 3 +++ tests/phpunit/tests/term/wpTerm.php | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/wp-includes/class-wp-term.php b/src/wp-includes/class-wp-term.php index acd0124172..76d74e060c 100644 --- a/src/wp-includes/class-wp-term.php +++ b/src/wp-includes/class-wp-term.php @@ -136,6 +136,9 @@ final class WP_Term { // If there isn't a cached version, hit the database. if ( ! $_term || ( $taxonomy && $taxonomy !== $_term->taxonomy ) ) { + // Any term found in the cache is not a match, so don't use it. + $_term = false; + // Grab all matching terms, in case any are shared between taxonomies. $terms = $wpdb->get_results( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE t.term_id = %d", $term_id ) ); if ( ! $terms ) { diff --git a/tests/phpunit/tests/term/wpTerm.php b/tests/phpunit/tests/term/wpTerm.php index 9154633f1e..5ccbc8beb2 100644 --- a/tests/phpunit/tests/term/wpTerm.php +++ b/tests/phpunit/tests/term/wpTerm.php @@ -68,4 +68,19 @@ class Tests_Term_WpTerm extends WP_UnitTestCase { $this->assertSame( 1, $found->term_id ); } + + /** + * @ticket 40671 + */ + public function test_get_instance_should_respect_taxonomy_when_term_id_is_found_in_cache() { + global $wpdb; + + register_taxonomy( 'wptests_tax2', 'post' ); + + // Ensure that cache is primed. + WP_Term::get_instance( self::$term_id, 'wptests_tax' ); + + $found = WP_Term::get_instance( self::$term_id, 'wptests_tax2' ); + $this->assertFalse( $found ); + } }