Make WordPress Core


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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.