diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index cf545fb7a3..0c7c0f03ab 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1574,11 +1574,11 @@ function get_term_to_edit( $id, $taxonomy ) { * @param array|string $args { * Optional. Array or string of arguments to get terms. * - * @type string $orderby Field(s) to order terms by. Accepts term fields ('name', 'slug', - * 'term_group', 'term_id', 'id'), 'count' for term taxonomy count, - * 'include' to match the 'order' of the $include param, or 'none' - * to skip ORDER BY. Defaults to 'name'. - * @type string $order Whether to order terms in ascending or descending order. + * @type string $orderby Field(s) to order terms by. Accepts term fields ('name', 'slug', + * 'term_group', 'term_id', 'id', 'description'), 'count' for term + * taxonomy count, 'include' to match the 'order' of the $include param, + * or 'none' to skip ORDER BY. Defaults to 'name'. + * @type string $order Whether to order terms in ascending or descending order. * Accepts 'ASC' (ascending) or 'DESC' (descending). * Default 'ASC'. * @type bool|int $hide_empty Whether to hide terms not assigned to any posts. Accepts @@ -1747,6 +1747,8 @@ function get_terms( $taxonomies, $args = '' ) { $orderby = "FIELD( t.term_id, $include )"; } elseif ( 'term_group' == $_orderby ) { $orderby = 't.term_group'; + } elseif ( 'description' == $_orderby ) { + $orderby = 'tt.description'; } elseif ( 'none' == $_orderby ) { $orderby = ''; } elseif ( empty($_orderby) || 'id' == $_orderby ) { diff --git a/tests/phpunit/tests/term/getTerms.php b/tests/phpunit/tests/term/getTerms.php index ae8e71d4b4..2dbbaffa5d 100644 --- a/tests/phpunit/tests/term/getTerms.php +++ b/tests/phpunit/tests/term/getTerms.php @@ -1135,6 +1135,29 @@ class Tests_Term_getTerms extends WP_UnitTestCase { $this->assertEquals( array( $t4, $t1, $t2 ), $found ); } + /** + * @ticket 31364 + */ + public function test_orderby_description() { + $tax = 'wptests_tax'; + register_taxonomy( $tax, 'post' ); + + $t1 = $this->factory->term->create( array( 'taxonomy' => $tax, 'description' => 'fff' ) ); + $t2 = $this->factory->term->create( array( 'taxonomy' => $tax, 'description' => 'aaa' ) ); + $t3 = $this->factory->term->create( array( 'taxonomy' => $tax, 'description' => 'zzz' ) ); + $t4 = $this->factory->term->create( array( 'taxonomy' => $tax, 'description' => 'jjj' ) ); + + $found = get_terms( $tax, array( + 'fields' => 'ids', + 'orderby' => 'description', + 'hide_empty' => false, + ) ); + + _unregister_taxonomy( 'wptests_tax' ); + + $this->assertEquals( array( $t2, $t1, $t4, $t3 ), $found ); + } + public function test_hierarchical_false_with_parent() { $initial_terms = $this->create_hierarchical_terms();