Make WordPress Core

Changeset 31236


Ignore:
Timestamp:
01/18/2015 06:56:38 PM (12 years ago)
Author:
boonebgorges
Message:

Additional 'orderby' values for wp_get_object_terms().

Adds support for ordering by 'taxonomy', 'term_taxonomy_id', and 'parent'.

Props ChriCo.
Fixes #28688.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r31232 r31236  
    25632563 *     Array of arguments.
    25642564 *     @type string $orderby Field by which results should be sorted. Accepts 'name', 'count', 'slug', 'term_group',
    2565  *                           or 'term_order'. Default 'name'.
     2565 *                           'term_order', 'taxonomy', 'parent', or 'term_taxonomy_id'. Default 'name'.
    25662566 *     @type string $order   Sort order. Accepts 'ASC' or 'DESC'. Default 'ASC'.
    25672567 *     @type string $fields  Fields to return for matched terms. Accepts 'all', 'ids', 'names', and
    … …  
    26132613        $fields = $args['fields'];
    26142614
    2615         if ( 'count' == $orderby ) {
    2616                 $orderby = 'tt.count';
    2617         } elseif ( 'name' == $orderby ) {
    2618                 $orderby = 't.name';
    2619         } elseif ( 'slug' == $orderby ) {
    2620                 $orderby = 't.slug';
    2621         } elseif ( 'term_group' == $orderby ) {
    2622                 $orderby = 't.term_group';
    2623         } elseif ( 'term_order' == $orderby ) {
     2615        if ( in_array( $orderby, array( 'term_id', 'name', 'slug', 'term_group' ) ) ) {
     2616                $orderby = "t.$orderby";
     2617        } else if ( in_array( $orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id' ) ) ) {
     2618                $orderby = "tt.$orderby";
     2619        } else if ( 'term_order' === $orderby ) {
    26242620                $orderby = 'tr.term_order';
    2625         } elseif ( 'none' == $orderby ) {
     2621        } else if ( 'none' === $orderby ) {
    26262622                $orderby = '';
    26272623                $order = '';
  • trunk/tests/phpunit/tests/term/wpGetObjectTerms.php

    r31233 r31236  
    228228        }
    229229
     230        /**
     231         * @ticket 28688
     232         */
     233        public function test_orderby_parent() {
     234                $p = $this->factory->post->create();
     235
     236                $t1 = $this->factory->term->create( array(
     237                        'taxonomy' => $this->taxonomy,
     238                ) );
     239                $t2 = $this->factory->term->create( array(
     240                        'taxonomy' => $this->taxonomy,
     241                ) );
     242                $t3 = $this->factory->term->create( array(
     243                        'taxonomy' => $this->taxonomy,
     244                ) );
     245
     246                $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
     247
     248                $term_1 = get_term( $t1, $this->taxonomy );
     249                $term_2 = get_term( $t2, $this->taxonomy );
     250                $term_3 = get_term( $t3, $this->taxonomy );
     251
     252                global $wpdb;
     253                $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 1 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id ) );
     254                $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 3 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) );
     255                $wpdb->update( $wpdb->term_taxonomy, array( 'parent' => 2 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) );
     256
     257                $found = wp_get_object_terms( $p, $this->taxonomy, array(
     258                        'orderby' => 'parent',
     259                        'fields' => 'ids',
     260                ) );
     261
     262                $this->assertEquals( array( $t1, $t3, $t2 ), $found );
     263        }
     264
     265        /**
     266         * @ticket 28688
     267         */
     268        public function test_orderby_taxonomy() {
     269                register_taxonomy( 'wptests_tax_2', 'post' );
     270                register_taxonomy( 'wptests_tax_3', 'post' );
     271
     272                $p = $this->factory->post->create();
     273
     274                $t1 = $this->factory->term->create( array(
     275                        'taxonomy' => $this->taxonomy,
     276                ) );
     277                $t2 = $this->factory->term->create( array(
     278                        'taxonomy' => 'wptests_tax_3',
     279                ) );
     280                $t3 = $this->factory->term->create( array(
     281                        'taxonomy' => 'wptests_tax_2',
     282                ) );
     283
     284                wp_set_object_terms( $p, $t1, $this->taxonomy );
     285                wp_set_object_terms( $p, $t2, 'wptests_tax_3' );
     286                wp_set_object_terms( $p, $t3, 'wptests_tax_2' );
     287
     288                $found = wp_get_object_terms( $p, array( $this->taxonomy, 'wptests_tax_2', 'wptests_tax_3' ), array(
     289                        'orderby' => 'taxonomy',
     290                        'fields' => 'ids',
     291                ) );
     292
     293                $this->assertEquals( array( $t1, $t3, $t2 ), $found );
     294        }
     295
     296        /**
     297         * @ticket 28688
     298         */
     299        public function test_orderby_tt_id() {
     300                $p = $this->factory->post->create();
     301
     302                $t1 = $this->factory->term->create( array(
     303                        'taxonomy' => $this->taxonomy,
     304                ) );
     305                $t2 = $this->factory->term->create( array(
     306                        'taxonomy' => $this->taxonomy,
     307                ) );
     308                $t3 = $this->factory->term->create( array(
     309                        'taxonomy' => $this->taxonomy,
     310                ) );
     311
     312                // term_taxonomy_id will only have a different order from term_id in legacy situations.
     313                $term_1 = get_term( $t1, $this->taxonomy );
     314                $term_2 = get_term( $t2, $this->taxonomy );
     315                $term_3 = get_term( $t3, $this->taxonomy );
     316
     317                global $wpdb;
     318                $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100004 ), array( 'term_taxonomy_id' => $term_1->term_taxonomy_id ) );
     319                $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100006 ), array( 'term_taxonomy_id' => $term_2->term_taxonomy_id ) );
     320                $wpdb->update( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => 100005 ), array( 'term_taxonomy_id' => $term_3->term_taxonomy_id ) );
     321
     322                $set = wp_set_object_terms( $p, array( $t1, $t2, $t3 ), $this->taxonomy );
     323
     324                $found = wp_get_object_terms( $p, $this->taxonomy, array(
     325                        'orderby' => 'term_taxonomy_id',
     326                        'fields' => 'ids',
     327                ) );
     328
     329                $this->assertEquals( array( $t1, $t3, $t2 ), $found );
     330        }
     331
    230332        public function test_order_desc() {
    231333                $p = $this->factory->post->create();
Note: See TracChangeset for help on using the changeset viewer.