From 6df24465f4b145080277fff3b7ca8c3d1b7db9d4 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 28 Oct 2014 18:12:30 +0000 Subject: [PATCH] Introduce `orderby=include` support for `get_terms()`. Props wpsmith. Fixes #23261. git-svn-id: https://develop.svn.wordpress.org/trunk@30052 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 3 +++ tests/phpunit/tests/term/getTerms.php | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) 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();