From 911c380a83c66c86944c28e70f08834342a273dd Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 10 Oct 2017 16:45:01 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy.php | 9 +++++- tests/phpunit/tests/term/query.php | 44 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index f2db5d81df..6313641d14 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1912,7 +1912,14 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { // Taxonomies registered without an 'args' param are handled here. 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 ); + } } /** diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index 855db6fb28..3d3379dfc2 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -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' ) ); } + /** + * @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 */