Fix termmeta pre-fetching in `wp_get_object_terms()`.

[34529] introduced logic intended to prime the termmeta cache for certain
values of the `fields` parameter. There were a few bugs:

* The `all_with_object_id` param was misspelled.
* `term_id` was used instead of `ids`.
* The values being passed to `update_termmeta_cache()` in the case where `fields=ids` was not correct.

All of these would result in a failure to pre-fetch termmeta in some cases.

Props dlh.
Fixes #36932.

git-svn-id: https://develop.svn.wordpress.org/trunk@37567 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges 2016-05-25 18:44:00 +00:00
parent c013727ebe
commit a378749df6
2 changed files with 61 additions and 3 deletions

View File

@ -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' );
}

View File

@ -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
*/