[wp-trac] [WordPress Trac] #28145: wp_terms_checklist() returns all non-hierarchical terms even with $descendants_and_self parameter

WordPress Trac noreply at wordpress.org
Tue May 6 07:55:52 UTC 2014


#28145: wp_terms_checklist() returns all non-hierarchical terms even with
$descendants_and_self parameter
-------------------------------+-----------------------------
 Reporter:  helgatheviking     |      Owner:
     Type:  defect (bug)       |     Status:  new
 Priority:  normal             |  Milestone:  Awaiting Review
Component:  Posts, Post Types  |    Version:  3.9
 Severity:  normal             |   Keywords:
  Focuses:                     |
-------------------------------+-----------------------------
 Granted, I know that `wp_terms_checklist()` is meant for hierarchical
 terms, but I am also using it on non-hierarchical terms in my Radio
 Buttons for Taxonomies plugin. I was trying to use all core scripts/ajax-
 callbacks for the "add new term" feature in a posts taxonomy metabox, but
 it turns out that `WP_Lists` calls `_wp_ajax_add_hierarchical_term` via
 ajax which returns `wp_terms_checklist()` as part of the result.

 For hierarchical terms the `descendants_and_self` parameter limits the
 size of the returned checklist. For non-hierarchical terms, however, I
 found that it simply returns *all* terms in addition to the new term.

 {{{
 wp_terms_checklist( $post_id, array('descendants_and_self' => $term_id ));
 }}}

 Obviously this isn't major, but it seems to me that if
 `descendants_and_self` is set, but there are no descendants... it should
 just return the single term.

 I've gotten around this by unhooking that ajax callback for non-
 hierarchical terms and substituting my own that returns just a single
 input.

 Here's the pertinent part of `wp_terms_checklist()`:

 {{{
 if ( $descendants_and_self ) {
                 $categories = (array) get_terms($taxonomy, array(
 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' =>
 0 ) );
                 $self = get_term( $descendants_and_self, $taxonomy );
                 array_unshift( $categories, $self );
         } else {
                 $categories = (array) get_terms($taxonomy, array('get' =>
 'all'));
         }
 }}}

 I think if for non-hierarchical terms we skipped the `get_terms()` call we
 could limit the returned checklist to a single term.

 {{{
 if ( $descendants_and_self ) {
                 $categories = is_taxonomy_hierarchical( $taxonomy ) ?
 (array) get_terms($taxonomy, array( 'child_of' => $descendants_and_self,
 'hierarchical' => 0, 'hide_empty' => 0 ) ) : array();
                 $self = get_term( $descendants_and_self, $taxonomy );
                 array_unshift( $categories, $self );
         } else {
                 $categories = (array) get_terms($taxonomy, array('get' =>
 'all'));
         }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/28145>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list