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
This commit is contained in:
Boone Gorges 2014-10-28 18:12:30 +00:00
parent 8a1bb7bba9
commit 6df24465f4
2 changed files with 27 additions and 0 deletions

View File

@ -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 ) {

View File

@ -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();