diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index fffc05f986..defc36cd8e 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1712,6 +1712,9 @@ function get_terms( $taxonomies, $args = '' ) { $orderby = 't.name'; } else if ( 'slug' == $_orderby ) { $orderby = 't.slug'; + } else if ( 'include' == $_orderby && ! empty( $args['include'] ) ) { + $include = implode( ',', array_map( 'absint', $args['include'] ) ); + $orderby = "FIELD( t.term_id, $include )"; } else if ( 'term_group' == $_orderby ) { $orderby = 't.term_group'; } else if ( 'none' == $_orderby ) { diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 47780ebf2b..f386796dd0 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -796,6 +796,30 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $this->assertEqualSets( $expected, $found ); } + /** + * @ticket 23261 + */ + public function test_orderby_include() { + $tax = 'wptests_tax'; + register_taxonomy( $tax, 'post' ); + + $t1 = $this->factory->term->create( array( 'taxonomy' => $tax ) ); + $t2 = $this->factory->term->create( array( 'taxonomy' => $tax ) ); + $t3 = $this->factory->term->create( array( 'taxonomy' => $tax ) ); + $t4 = $this->factory->term->create( array( 'taxonomy' => $tax ) ); + + $found = get_terms( $tax, array( + 'fields' => 'ids', + 'include' => array( $t4, $t1, $t2 ), + 'orderby' => 'include', + 'hide_empty' => false, + ) ); + + _unregister_taxonomy( 'wptests_tax' ); + + $this->assertEquals( array( $t4, $t1, $t2 ), $found ); + } + protected function create_hierarchical_terms_and_posts() { $terms = array();