From 9501fb321345d9c93df2f1efc4239186d2b5565c Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sun, 18 Jan 2015 18:56:38 +0000 Subject: [PATCH] Additional 'orderby' values for `wp_get_object_terms()`. Adds support for ordering by 'taxonomy', 'term_taxonomy_id', and 'parent'. Props ChriCo. Fixes #28688. git-svn-id: https://develop.svn.wordpress.org/trunk@31236 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 18 ++-- tests/phpunit/tests/term/wpGetObjectTerms.php | 102 ++++++++++++++++++ 2 files changed, 109 insertions(+), 11 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index ba70344253..9506a347db 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2562,7 +2562,7 @@ function wp_delete_category( $cat_ID ) { * @param array|string $args { * Array of arguments. * @type string $orderby Field by which results should be sorted. Accepts 'name', 'count', 'slug', 'term_group', - * or 'term_order'. Default 'name'. + * 'term_order', 'taxonomy', 'parent', or 'term_taxonomy_id'. Default 'name'. * @type string $order Sort order. Accepts 'ASC' or 'DESC'. Default 'ASC'. * @type string $fields Fields to return for matched terms. Accepts 'all', 'ids', 'names', and * 'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result in an array of @@ -2612,17 +2612,13 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { $order = $args['order']; $fields = $args['fields']; - if ( 'count' == $orderby ) { - $orderby = 'tt.count'; - } elseif ( 'name' == $orderby ) { - $orderby = 't.name'; - } elseif ( 'slug' == $orderby ) { - $orderby = 't.slug'; - } elseif ( 'term_group' == $orderby ) { - $orderby = 't.term_group'; - } elseif ( 'term_order' == $orderby ) { + if ( in_array( $orderby, array( 'term_id', 'name', 'slug', 'term_group' ) ) ) { + $orderby = "t.$orderby"; + } else if ( in_array( $orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id' ) ) ) { + $orderby = "tt.$orderby"; + } else if ( 'term_order' === $orderby ) { $orderby = 'tr.term_order'; - } elseif ( 'none' == $orderby ) { + } else if ( 'none' === $orderby ) { $orderby = ''; $order = ''; } else { diff --git a/tests/phpunit/tests/term/wpGetObjectTerms.php b/tests/phpunit/tests/term/wpGetObjectTerms.php index 1076e982ae..7f9a43d99e 100644 --- a/tests/phpunit/tests/term/wpGetObjectTerms.php +++ b/tests/phpunit/tests/term/wpGetObjectTerms.php @@ -227,6 +227,108 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { $this->assertEquals( array( $t1, $t3, $t2 ), $found ); } + /** + * @ticket 28688 + */ + public function test_orderby_parent() { + $p = $this->factory->post->create(); + + $t1 = $this->factory->term->create( array( + 'taxonomy' => $this->taxonomy, + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => $this->taxonomy, + ) ); + $t3 = $this->factory->term->create( array( + 'taxonomy' => $this->taxonomy, + ) ); + + $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy ); + + $term_1 = get_term( $t1, $this->taxonomy ); + $term_2 = get_term( $t2, $this->taxonomy ); + $term_3 = get_term( $t3, $this->taxonomy ); + + global $wpdb; + $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 1 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id ) ); + $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 3 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) ); + $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 2 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) ); + + $found = wp_get_object_terms( $p, $this->taxonomy, array( + 'orderby' => 'parent', + 'fields' => 'ids', + ) ); + + $this->assertEquals( array( $t1, $t3, $t2 ), $found ); + } + + /** + * @ticket 28688 + */ + public function test_orderby_taxonomy() { + register_taxonomy( 'wptests_tax_2', 'post' ); + register_taxonomy( 'wptests_tax_3', 'post' ); + + $p = $this->factory->post->create(); + + $t1 = $this->factory->term->create( array( + 'taxonomy' => $this->taxonomy, + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax_3', + ) ); + $t3 = $this->factory->term->create( array( + 'taxonomy' => 'wptests_tax_2', + ) ); + + wp_set_object_terms( $p, $t1, $this->taxonomy ); + wp_set_object_terms( $p, $t2, 'wptests_tax_3' ); + wp_set_object_terms( $p, $t3, 'wptests_tax_2' ); + + $found = wp_get_object_terms( $p, array( $this->taxonomy, 'wptests_tax_2', 'wptests_tax_3' ), array( + 'orderby' => 'taxonomy', + 'fields' => 'ids', + ) ); + + $this->assertEquals( array( $t1, $t3, $t2 ), $found ); + } + + /** + * @ticket 28688 + */ + public function test_orderby_tt_id() { + $p = $this->factory->post->create(); + + $t1 = $this->factory->term->create( array( + 'taxonomy' => $this->taxonomy, + ) ); + $t2 = $this->factory->term->create( array( + 'taxonomy' => $this->taxonomy, + ) ); + $t3 = $this->factory->term->create( array( + 'taxonomy' => $this->taxonomy, + ) ); + + // term_taxonomy_id will only have a different order from term_id in legacy situations. + $term_1 = get_term( $t1, $this->taxonomy ); + $term_2 = get_term( $t2, $this->taxonomy ); + $term_3 = get_term( $t3, $this->taxonomy ); + + global $wpdb; + $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100004 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id ) ); + $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100006 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) ); + $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100005 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) ); + + $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy ); + + $found = wp_get_object_terms( $p, $this->taxonomy, array( + 'orderby' => 'term_taxonomy_id', + 'fields' => 'ids', + ) ); + + $this->assertEquals( array( $t1, $t3, $t2 ), $found ); + } + public function test_order_desc() { $p = $this->factory->post->create();