[wp-trac] [WordPress Trac] #29839: Enhance get_terms to return only bottom child terms (terms without children) when taxonomy is heirarchical

WordPress Trac noreply at wordpress.org
Sun Nov 16 19:11:06 UTC 2014


#29839: Enhance get_terms to return only bottom child terms (terms without
children) when taxonomy is heirarchical
------------------------------------------------+--------------------------
 Reporter:  theMikeD                            |       Owner:
     Type:  enhancement                         |      Status:  new
 Priority:  normal                              |   Milestone:  Future
Component:  Taxonomy                            |  Release
 Severity:  normal                              |     Version:  4.0
 Keywords:  4.2-early good-first-bug has-patch  |  Resolution:
                                                |     Focuses:
------------------------------------------------+--------------------------

Comment (by theMikeD):

 I've attached an updated unit test diff because I'v run into a problem
 that I can't figure out.

 The first set of test deal with an hierarchical taxonomy, and they all
 pass, including the new one that verifies that `('get' => 'all)' works as
 expected.

 The problem is that I then go on to test a flat taxonomy, and the act of
 assigning terms to a post actually creates new terms, and I can't figure
 out why. The code in question is


 {{{
                 $flat_tax = 'countries';
                 register_taxonomy( $flat_tax, 'post', array(
 'hierarchical' => false ) );
                 $australia = $this->factory->term->create( array( 'name'
 => 'Australia', 'taxonomy' => $flat_tax ) );
                 $china  =    $this->factory->term->create( array( 'name'
 => 'China',     'taxonomy' => $flat_tax ) );
                 $tanzania =  $this->factory->term->create( array( 'name'
 => 'Tanzania',  'taxonomy' => $flat_tax ) );

                 $post4_id = $this->factory->post->create();
                 $post5_id = $this->factory->post->create();


                 // Check 1
                 $terms = get_terms( $flat_tax, array( 'childless' => true,
 'hide_empty' => false) );
                 error_log( print_r( $terms ));
                 wp_set_post_terms( $post4_id, $montreal, $flat_tax );
                 wp_set_post_terms( $post5_id, $ottawa, $flat_tax );
                 // Check 2
                 $terms = get_terms( $flat_tax, array( 'childless' => true,
 'hide_empty' => false) );
                 error_log( print_r( $terms ));
 }}}


 The results of Check 1 and 2 should be the same, but they are not. Check 1
 shows the three terms as expected, but Check 2 shows the three terms plus
 two more that are just numbers, not text. For example

 Check 1

 {{{
 Array
 (
     [0] => stdClass Object
         (
             [term_id] => 10
             [name] => Australia
             [slug] => australia
             [term_group] => 0
             [term_taxonomy_id] => 10
             [taxonomy] => countries
             [description] => Term description 9
             [parent] => 0
             [count] => 0
         )

     [1] => stdClass Object
         (
             [term_id] => 11
             [name] => China
             [slug] => china
             [term_group] => 0
             [term_taxonomy_id] => 11
             [taxonomy] => countries
             [description] => Term description 10
             [parent] => 0
             [count] => 0
         )

     [2] => stdClass Object
         (
             [term_id] => 12
             [name] => Tanzania
             [slug] => tanzania
             [term_group] => 0
             [term_taxonomy_id] => 12
             [taxonomy] => countries
             [description] => Term description 11
             [parent] => 0
             [count] => 0
         )

 )
 }}}


 Check 2


 {{{
 Array
 (
     [0] => stdClass Object
         (
             [term_id] => 14
             [name] => 7
             [slug] => 7
             [term_group] => 0
             [term_taxonomy_id] => 14
             [taxonomy] => countries
             [description] =>
             [parent] => 0
             [count] => 1
         )

     [1] => stdClass Object
         (
             [term_id] => 13
             [name] => 8
             [slug] => 8
             [term_group] => 0
             [term_taxonomy_id] => 13
             [taxonomy] => countries
             [description] =>
             [parent] => 0
             [count] => 1
         )

     [2] => stdClass Object
         (
             [term_id] => 10
             [name] => Australia
             [slug] => australia
             [term_group] => 0
             [term_taxonomy_id] => 10
             [taxonomy] => countries
             [description] => Term description 9
             [parent] => 0
             [count] => 0
         )

     [3] => stdClass Object
         (
             [term_id] => 11
             [name] => China
             [slug] => china
             [term_group] => 0
             [term_taxonomy_id] => 11
             [taxonomy] => countries
             [description] => Term description 10
             [parent] => 0
             [count] => 0
         )

     [4] => stdClass Object
         (
             [term_id] => 12
             [name] => Tanzania
             [slug] => tanzania
             [term_group] => 0
             [term_taxonomy_id] => 12
             [taxonomy] => countries
             [description] => Term description 11
             [parent] => 0
             [count] => 0
         )

 )

 }}}

 This is implying that the `wp_set_post_terms` is adding the bad terms, but
 I don't understand why because the arguments seems fine: the first is `6,
 11, countries` and the second is `7, 12, countries`.

 This false term insertion makes all assertions fail.

 Does anything jump out at you? I'm going around in circles.

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


More information about the wp-trac mailing list