[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 15:31:50 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):

 Replying to [comment:34 boonebgorges]:
 > > I bomb out for the same reason that parent does: there is no such
 thing as a child term if there is no hierarchy. If I didn't then this
 option would be the same as all which doesn't sit with me.
 >
 > Hm. In cases where the hierarchy is flat, technically *all* nodes are
 leaf nodes. So my inclination would be to return all items.

 There's def. two ways to look at it. Personally I think returning all
 terms would lead to confusing results for users who accidentally run this
 option on a non-hierarchical taxonomy. How are such differences of opinion
 handled in a situation like this? In any case, it's an easy fix: change


 {{{
         $childless = (bool) $args['childless']; // boolean
         if ( $childless ) {
                 // Empty if taxonomy is not hierarchical; term ids if it
 is.
                 // Note that we are not checking for a specific child or
 parent, just the
                 //  existence of an hierarchical taxonomy
                 $hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
                 if ( empty( $hierarchy )) {
                         return $empty_array;
                 }
         }

 }}}

 to


 {{{
         $childless = (bool) $args['childless']; // boolean
         if ( $childless ) {
                 // Empty if taxonomy is not hierarchical; term ids if it
 is.
                 // Note that we are not checking for a specific child or
 parent, just the
                 //  existence of an hierarchical taxonomy
                 $hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
                 if ( empty( $hierarchy )) {
                         $args['get'] = 'all';
                 }
         }

 }}}

 and move it up above the check for `all` around line 1663.


 > It's not handled anywhere that I can see. This code is quite old and not
 necessarily up to standards, so don't use it as a model here. That being
 said, I think it's OK to accept 0 as an option here - just cast to bool at
 some point.

 I think the best spot to do this would be here:

 {{{
 $childless = $args['childless']; // boolean
 }}}

 changing it to

 {{{
 $childless = (bool) $args['childless']; // boolean
 }}}

 According to the PHP info (
 http://php.net/manual/en/language.types.boolean.php ) this is done
 automatically when a var is used in boolean context (as this one is) but,
 like the `intval` done earlier, better safe right?

 Also, there's this block

 {{{
         if ( 'all' == $args['get'] ) {
                 $args['childless'] = 0;
                 $args['child_of'] = 0;
                 $args['hide_empty'] = 0;
                 $args['hierarchical'] = false;
                 $args['pad_counts'] = false;
         }

 }}}

 that does a mixture. Is it kosher to fix them all to be one or the other,
 or should that be a separate issue?

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


More information about the wp-trac mailing list