Skip to content

Commit e6ee032

Browse files
author
boonebgorges
committed
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
1 parent 1860ad1 commit e6ee032

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/wp-includes/taxonomy.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,9 +2628,9 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
26282628
}
26292629

26302630
// Update termmeta cache, if necessary.
2631-
if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_ids' === $fields || 'term_id' === $fields ) ) {
2632-
if ( 'term_id' === $fields ) {
2633-
$term_ids = $fields;
2631+
if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_id' === $fields || 'ids' === $fields ) ) {
2632+
if ( 'ids' === $fields ) {
2633+
$term_ids = $terms;
26342634
} else {
26352635
$term_ids = wp_list_pluck( $terms, 'term_id' );
26362636
}

tests/phpunit/tests/term/wpGetObjectTerms.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,64 @@ public function test_termmeta_cache_should_not_be_primed_when_update_term_meta_c
472472
$this->assertSame( $num_queries + 3, $wpdb->num_queries );
473473
}
474474

475+
/**
476+
* @ticket 36932
477+
*/
478+
public function test_termmeta_cache_should_be_primed_when_fields_is_all_with_object_id() {
479+
global $wpdb;
480+
481+
register_taxonomy( 'wptests_tax', 'post' );
482+
$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
483+
add_term_meta( $terms[0], 'foo', 'bar' );
484+
add_term_meta( $terms[1], 'foo', 'bar' );
485+
add_term_meta( $terms[2], 'foo', 'bar' );
486+
487+
$p = self::factory()->post->create();
488+
wp_set_object_terms( $p, $terms, 'wptests_tax' );
489+
490+
$found = wp_get_object_terms( $p, 'wptests_tax', array(
491+
'update_term_meta_cache' => true,
492+
'fields' => 'all_with_object_id',
493+
) );
494+
495+
$num_queries = $wpdb->num_queries;
496+
497+
foreach ( $terms as $t ) {
498+
$this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
499+
}
500+
501+
$this->assertSame( $num_queries, $wpdb->num_queries );
502+
}
503+
504+
/**
505+
* @ticket 36932
506+
*/
507+
public function test_termmeta_cache_should_be_primed_when_fields_is_ids() {
508+
global $wpdb;
509+
510+
register_taxonomy( 'wptests_tax', 'post' );
511+
$terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) );
512+
add_term_meta( $terms[0], 'foo', 'bar' );
513+
add_term_meta( $terms[1], 'foo', 'bar' );
514+
add_term_meta( $terms[2], 'foo', 'bar' );
515+
516+
$p = self::factory()->post->create();
517+
wp_set_object_terms( $p, $terms, 'wptests_tax' );
518+
519+
$found = wp_get_object_terms( $p, 'wptests_tax', array(
520+
'update_term_meta_cache' => true,
521+
'fields' => 'ids',
522+
) );
523+
524+
$num_queries = $wpdb->num_queries;
525+
526+
foreach ( $terms as $t ) {
527+
$this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) );
528+
}
529+
530+
$this->assertSame( $num_queries, $wpdb->num_queries );
531+
}
532+
475533
/**
476534
* @ticket 10142
477535
*/

0 commit comments

Comments
 (0)