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