diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index a60052c2da..ad3fba6714 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2628,9 +2628,9 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { } // Update termmeta cache, if necessary. - if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_ids' === $fields || 'term_id' === $fields ) ) { - if ( 'term_id' === $fields ) { - $term_ids = $fields; + if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_id' === $fields || 'ids' === $fields ) ) { + if ( 'ids' === $fields ) { + $term_ids = $terms; } else { $term_ids = wp_list_pluck( $terms, 'term_id' ); } diff --git a/tests/phpunit/tests/term/wpGetObjectTerms.php b/tests/phpunit/tests/term/wpGetObjectTerms.php index ffa87817d6..892e7a1524 100644 --- a/tests/phpunit/tests/term/wpGetObjectTerms.php +++ b/tests/phpunit/tests/term/wpGetObjectTerms.php @@ -472,6 +472,64 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { $this->assertSame( $num_queries + 3, $wpdb->num_queries ); } + /** + * @ticket 36932 + */ + public function test_termmeta_cache_should_be_primed_when_fields_is_all_with_object_id() { + global $wpdb; + + register_taxonomy( 'wptests_tax', 'post' ); + $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); + add_term_meta( $terms[0], 'foo', 'bar' ); + add_term_meta( $terms[1], 'foo', 'bar' ); + add_term_meta( $terms[2], 'foo', 'bar' ); + + $p = self::factory()->post->create(); + wp_set_object_terms( $p, $terms, 'wptests_tax' ); + + $found = wp_get_object_terms( $p, 'wptests_tax', array( + 'update_term_meta_cache' => true, + 'fields' => 'all_with_object_id', + ) ); + + $num_queries = $wpdb->num_queries; + + foreach ( $terms as $t ) { + $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); + } + + $this->assertSame( $num_queries, $wpdb->num_queries ); + } + + /** + * @ticket 36932 + */ + public function test_termmeta_cache_should_be_primed_when_fields_is_ids() { + global $wpdb; + + register_taxonomy( 'wptests_tax', 'post' ); + $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); + add_term_meta( $terms[0], 'foo', 'bar' ); + add_term_meta( $terms[1], 'foo', 'bar' ); + add_term_meta( $terms[2], 'foo', 'bar' ); + + $p = self::factory()->post->create(); + wp_set_object_terms( $p, $terms, 'wptests_tax' ); + + $found = wp_get_object_terms( $p, 'wptests_tax', array( + 'update_term_meta_cache' => true, + 'fields' => 'ids', + ) ); + + $num_queries = $wpdb->num_queries; + + foreach ( $terms as $t ) { + $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); + } + + $this->assertSame( $num_queries, $wpdb->num_queries ); + } + /** * @ticket 10142 */