[wp-trac] [WordPress Trac] #40496: get_the_terms() doesn't respect register_taxonomy()'s 'orderby' => 'term_order'

WordPress Trac noreply at wordpress.org
Fri Apr 21 18:18:59 UTC 2017


#40496: get_the_terms() doesn't respect register_taxonomy()'s 'orderby' =>
'term_order'
-------------------------------------+--------------------
 Reporter:  danielbachhuber          |       Owner:
     Type:  defect (bug)             |      Status:  new
 Priority:  normal                   |   Milestone:  4.7.5
Component:  Taxonomy                 |     Version:  4.7
 Severity:  normal                   |  Resolution:
 Keywords:  has-patch needs-testing  |     Focuses:
-------------------------------------+--------------------

Comment (by danielbachhuber):

 > In my (lame) defense, I had no idea that this weird feature existed - it
 was introduced in [7520], doesn't appear to be documented anywhere, and is
 a pattern I've never seen used elsewhere in WordPress.

 Undocumented features are the best features.

 > 40496.diff​ appears to resolve the problem by restoring the argument-
 merging from pre-4.7. @danielbachhuber Care to have a look?

 Good thing I tested! It fixes the problem for querying one taxonomy, but
 breaks for querying two taxonomies with `wp_get_object_terms()`.

 Here's the test that fails:

 {{{
 public function test_wp_get_object_terms_should_respect_taxonomy_orderby()
 {
         register_taxonomy( 'wptests_tax', 'post', array(
                 'sort' => true,
                 'args' => array(
                         'orderby' => 'term_order',
                 ),
         ) );
         $term_ids = self::factory()->term->create_many( 2, array(
                 'taxonomy' => 'wptests_tax',
         ) );
         $post_id = self::factory()->post->create();
         wp_set_object_terms( $post_id, array( $term_ids[0], $term_ids[1]
 ), 'wptests_tax' );
         $terms = wp_get_object_terms( $post_id, array( 'category',
 'wptests_tax' ) );
         $this->assertEquals( array( $term_ids[0], $term_ids[1] ),
 wp_list_pluck( $terms, 'term_id' ) );
         // Flip the order
         wp_set_object_terms( $post_id, array( $term_ids[1], $term_ids[0]
 ), 'wptests_tax' );
         $terms = wp_get_object_terms( $post_id, array( 'category',
 'wptests_tax' ) );
         $this->assertEquals( array( $term_ids[1], $term_ids[0] ),
 wp_list_pluck( $terms, 'term_id' ) );
 }
 }}}

 The original code set `$terms = array()` early, then called
 `wp_get_object_terms()` for each taxonomy and merged the terms in.

 The code in 4.7 sets `$terms = get_terms( $args );` after the code you've
 ported over, so the handling for two taxonomies is discarded.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/40496#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list