Taxonomy: Don't discard keys when merging queried terms from different taxonomies.
For values of `fields` like `id=>parent`, the keys of the array must be maintained as part of the query results. Introduced as part of #40496. See [38667], [40513]. Props miyauchi, dany2217, pcarvalho. Fixes #41293. git-svn-id: https://develop.svn.wordpress.org/trunk@41809 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
0bf42bf3b7
commit
911c380a83
|
@ -1912,7 +1912,14 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
|
||||||
|
|
||||||
// Taxonomies registered without an 'args' param are handled here.
|
// Taxonomies registered without an 'args' param are handled here.
|
||||||
if ( ! empty( $taxonomies ) ) {
|
if ( ! empty( $taxonomies ) ) {
|
||||||
$terms = array_merge( $terms, get_terms( $args ) );
|
$terms_from_remaining_taxonomies = get_terms( $args );
|
||||||
|
|
||||||
|
// Array keys should be preserved for values of $fields that use term_id for keys.
|
||||||
|
if ( ! empty( $args['fields'] ) && 0 === strpos( $args['fields'], 'id=>' ) ) {
|
||||||
|
$terms = $terms + $terms_from_remaining_taxonomies;
|
||||||
|
} else {
|
||||||
|
$terms = array_merge( $terms, $terms_from_remaining_taxonomies );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -428,6 +428,50 @@ class Tests_Term_Query extends WP_UnitTestCase {
|
||||||
$this->assertEquals( array( $term_ids[1], $term_ids[0], 1 ), wp_list_pluck( $terms, 'term_id' ) );
|
$this->assertEquals( array( $term_ids[1], $term_ids[0], 1 ), wp_list_pluck( $terms, 'term_id' ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 41293
|
||||||
|
*/
|
||||||
|
public function test_should_allow_same_args_with_the_get_terms() {
|
||||||
|
register_post_type( 'wptests_pt' );
|
||||||
|
register_taxonomy( 'wptests_tax', 'wptests_pt' );
|
||||||
|
$t1 = self::factory()->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
'name' => 'foo',
|
||||||
|
'slug' => 'bar',
|
||||||
|
) );
|
||||||
|
$t2 = self::factory()->term->create( array(
|
||||||
|
'taxonomy' => 'wptests_tax',
|
||||||
|
'name' => 'bar',
|
||||||
|
'slug' => 'foo',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$p = self::factory()->post->create( array(
|
||||||
|
'post_type' => 'wptests_pt',
|
||||||
|
) );
|
||||||
|
|
||||||
|
wp_set_object_terms( $p, array( $t1, $t2 ), 'wptests_tax' );
|
||||||
|
|
||||||
|
$expected = wp_get_post_terms( $p, 'wptests_tax', array(
|
||||||
|
'fields' => 'ids',
|
||||||
|
) );
|
||||||
|
|
||||||
|
$found1 = array_keys( wp_get_object_terms( $p, 'wptests_tax', array(
|
||||||
|
'fields' => 'id=>parent',
|
||||||
|
) ) );
|
||||||
|
|
||||||
|
$found2 = array_keys( wp_get_object_terms( $p, 'wptests_tax', array(
|
||||||
|
'fields' => 'id=>slug',
|
||||||
|
) ) );
|
||||||
|
|
||||||
|
$found3 = array_keys( wp_get_object_terms( $p, 'wptests_tax', array(
|
||||||
|
'fields' => 'id=>name',
|
||||||
|
) ) );
|
||||||
|
|
||||||
|
$this->assertSame( $expected, $found1 );
|
||||||
|
$this->assertSame( $expected, $found2 );
|
||||||
|
$this->assertSame( $expected, $found3 );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ticket 41796
|
* @ticket 41796
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue