diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 94f36689fd..f1c1bce307 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1841,10 +1841,30 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { $args = wp_parse_args( $args ); + /* + * When one or more queried taxonomies is registered with an 'args' array, + * those params override the `$args` passed to this function. + */ + $terms = array(); + if ( count( $taxonomies ) > 1 ) { + foreach ( $taxonomies as $index => $taxonomy ) { + $t = get_taxonomy( $taxonomy ); + if ( isset( $t->args ) && is_array( $t->args ) && $args != array_merge( $args, $t->args ) ) { + unset( $taxonomies[ $index ] ); + $terms = array_merge( $terms, wp_get_object_terms( $object_ids, $taxonomy, array_merge( $args, $t->args ) ) ); + } + } + } else { + $t = get_taxonomy( $taxonomies[0] ); + if ( isset( $t->args ) && is_array( $t->args ) ) { + $args = array_merge( $args, $t->args ); + } + } + $args['taxonomy'] = $taxonomies; $args['object_ids'] = $object_ids; - $terms = get_terms( $args ); + $terms = array_merge( $terms, get_terms( $args ) ); /** * Filters the terms for a given object or objects. diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index 4a15f1e53f..b2e2a27621 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -381,4 +381,50 @@ class Tests_Term_Query extends WP_UnitTestCase { $count = $query->get_terms(); $this->assertEquals( 1, $count ); } + + /** + * @ticket 40496 + */ + public function test_get_the_terms_should_respect_taxonomy_orderby() { + register_taxonomy( 'wptests_tax', 'post', array( + 'sort' => true, + 'args' => array( + 'orderby' => 'term_order', + ), + ) ); + $term_ids = self::factory()->term->create_many( 2, array( + 'taxonomy' => 'wptests_tax', + ) ); + $post_id = self::factory()->post->create(); + wp_set_object_terms( $post_id, array( $term_ids[0], $term_ids[1] ), 'wptests_tax' ); + $terms = get_the_terms( $post_id, 'wptests_tax' ); + $this->assertEquals( array( $term_ids[0], $term_ids[1] ), wp_list_pluck( $terms, 'term_id' ) ); + // Flip the order + wp_set_object_terms( $post_id, array( $term_ids[1], $term_ids[0] ), 'wptests_tax' ); + $terms = get_the_terms( $post_id, 'wptests_tax' ); + $this->assertEquals( array( $term_ids[1], $term_ids[0] ), wp_list_pluck( $terms, 'term_id' ) ); + } + + /** + * @ticket 40496 + */ + public function test_wp_get_object_terms_should_respect_taxonomy_orderby() { + register_taxonomy( 'wptests_tax', 'post', array( + 'sort' => true, + 'args' => array( + 'orderby' => 'term_order', + ), + ) ); + $term_ids = self::factory()->term->create_many( 2, array( + 'taxonomy' => 'wptests_tax', + ) ); + $post_id = self::factory()->post->create(); + wp_set_object_terms( $post_id, array( $term_ids[0], $term_ids[1] ), 'wptests_tax' ); + $terms = wp_get_object_terms( $post_id, array( 'category', 'wptests_tax' ) ); + $this->assertEquals( array( $term_ids[0], $term_ids[1], 1 ), wp_list_pluck( $terms, 'term_id' ) ); + // Flip the order + wp_set_object_terms( $post_id, array( $term_ids[1], $term_ids[0] ), 'wptests_tax' ); + $terms = wp_get_object_terms( $post_id, array( 'category', 'wptests_tax' ) ); + $this->assertEquals( array( $term_ids[1], $term_ids[0], 1 ), wp_list_pluck( $terms, 'term_id' ) ); + } }