Make WordPress Core


Ignore:
Timestamp:
01/24/2015 06:47:30 PM (9 years ago)
Author:
boonebgorges
Message:

Introduce 'childless' parameter to get_terms().

This new parameter allows developers to limit queried terms to terminal nodes -
ie, those without any descendants.

As part of the improvement, some internal logic in get_terms() has been
consolidated. Parameters that resolve to a NOT IN clause containing term IDs
('exclude', 'exclude_tree', and 'childless') are now parsed into a single
"exclusions" array before the SQL clause is generated.

Props theMikeD, horike.
Fixes #29839.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/taxonomy.php

    r31270 r31275  
    15481548 *
    15491549 * @since 2.3.0
    1550  * @since 4.2.0 Introduced 'name' parameter.
     1550 * @since 4.2.0 Introduced 'name' and 'childless' parameters.
    15511551 *
    15521552 * @global wpdb $wpdb WordPress database abstraction object.
     
    15961596 *                                           are passed, $child_of is ignored. Default 0.
    15971597 *     @type int|string   $parent            Parent term ID to retrieve direct-child terms of. Default empty.
     1598 *     @type bool         $childless         True to limit results to terms that have no children. This parameter has
     1599 *                                           no effect on non-hierarchical taxonomies. Default false.
    15981600 *     @type string       $cache_domain      Unique cache key to be produced when this query is stored in an
    15991601 *                                           object cache. Default is 'core'.
     
    16201622    $defaults = array('orderby' => 'name', 'order' => 'ASC',
    16211623        'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
    1622         'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '',
     1624        'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 'childless' => false,
    16231625        'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
    16241626        'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
     
    16391641
    16401642    if ( 'all' == $args['get'] ) {
     1643        $args['childless'] = false;
    16411644        $args['child_of'] = 0;
    16421645        $args['hide_empty'] = 0;
     
    17551758    }
    17561759
     1760    $exclusions = array();
    17571761    if ( ! empty( $exclude_tree ) ) {
    17581762        $exclude_tree = wp_parse_id_list( $exclude_tree );
     
    17641768            );
    17651769        }
    1766         $exclusions = implode( ',', array_map( 'intval', $excluded_children ) );
    1767     } else {
    1768         $exclusions = '';
     1770        $exclusions = array_merge( $excluded_children, $exclusions );
    17691771    }
    17701772
    17711773    if ( ! empty( $exclude ) ) {
    1772         $exterms = wp_parse_id_list( $exclude );
    1773         if ( empty( $exclusions ) ) {
    1774             $exclusions = implode( ',', $exterms );
    1775         } else {
    1776             $exclusions .= ', ' . implode( ',', $exterms );
     1774        $exclusions = array_merge( wp_parse_id_list( $exclude ), $exclusions );
     1775    }
     1776
     1777    // 'childless' terms are those without an entry in the flattened term hierarchy.
     1778    $childless = (bool) $args['childless'];
     1779    if ( $childless ) {
     1780        foreach ( $taxonomies as $_tax ) {
     1781            $term_hierarchy = _get_term_hierarchy( $_tax );
     1782            $exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions );
    17771783        }
    17781784    }
    17791785
    17801786    if ( ! empty( $exclusions ) ) {
    1781         $exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
     1787        $exclusions = ' AND t.term_id NOT IN (' . implode( ',', array_map( 'intval', $exclusions ) ) . ')';
    17821788    }
    17831789
Note: See TracChangeset for help on using the changeset viewer.