From 9dfc5551eebe80d40d0bcc88aecc6bb426cd89e2 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 4 Sep 2015 21:16:11 +0000 Subject: [PATCH] In `get_terms()`, allow terms to be ordered by 'term_id'. [29128] introduced updated documentation for the `'orderby'` parameter of `get_terms()`. The new documentation mistakenly said that 'term_id' was a valid orderby value. The current changeset makes that fantasy...A REALITY. Props ixkaito. Fixes #33726. git-svn-id: https://develop.svn.wordpress.org/trunk@33903 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy-functions.php | 3 ++- tests/phpunit/tests/term/getTerms.php | 27 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/taxonomy-functions.php b/src/wp-includes/taxonomy-functions.php index 3e43f78479..8188a2ef7e 100644 --- a/src/wp-includes/taxonomy-functions.php +++ b/src/wp-includes/taxonomy-functions.php @@ -945,6 +945,7 @@ function get_term_to_edit( $id, $taxonomy ) { * * @since 2.3.0 * @since 4.2.0 Introduced 'name' and 'childless' parameters. + * @since 4.4.0 Introduced the ability to pass 'term_id' as an alias of 'id' for the `orderby` parameter. * * @global wpdb $wpdb WordPress database abstraction object. * @global array $wp_filter @@ -1132,7 +1133,7 @@ function get_terms( $taxonomies, $args = '' ) { $orderby = 'tt.description'; } elseif ( 'none' == $_orderby ) { $orderby = ''; - } elseif ( empty($_orderby) || 'id' == $_orderby ) { + } elseif ( empty( $_orderby ) || 'id' == $_orderby || 'term_id' === $_orderby ) { $orderby = 't.term_id'; } else { $orderby = 't.name'; diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index 333e87ec6a..b1706488cd 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -1186,6 +1186,33 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $this->assertEquals( array( $t2, $t1, $t4, $t3 ), $found ); } + /** + * @ticket 33726 + */ + public function test_orderby_term_id() { + register_taxonomy( 'wptests_tax', 'post' ); + $t1 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + 'name' => 'AAA', + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + 'name' => 'ZZZ', + ) ); + $t3 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax', + 'name' => 'JJJ', + ) ); + + $found = get_terms( 'wptests_tax', array( + 'orderby' => 'term_id', + 'hide_empty' => false, + 'fields' => 'ids', + ) ); + + $this->assertEquals( array( $t1, $t2, $t3 ), $found ); + } + public function test_hierarchical_false_with_parent() { $initial_terms = $this->create_hierarchical_terms();