Make WordPress Core

Ticket #29839: taxonomy.diff

File taxonomy.diff, 2.9 KB (added by theMikeD, 11 years ago)

Fix for #29839

  • taxonomy.php

     
    16401640
    16411641        $defaults = array('orderby' => 'name', 'order' => 'ASC',
    16421642                'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
    1643                 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
     1643                'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '', 'children_only' => '',
    16441644                'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
    16451645                'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
    16461646        $args = wp_parse_args( $args, $defaults );
     
    16531653                $args['pad_counts'] = false;
    16541654        }
    16551655
    1656         // 'parent' overrides 'child_of'.
    1657         if ( 0 < intval( $args['parent'] ) ) {
     1656        // 'parent' and 'children_only' override 'child_of'
     1657        if ( 0 < intval( $args['parent'] ) || true === $args['children_only'] ) {
    16581658                $args['child_of'] = false;
    16591659        }
    16601660
    16611661        if ( 'all' == $args['get'] ) {
     1662                $args['children_only'] = 0;
    16621663                $args['child_of'] = 0;
    16631664                $args['hide_empty'] = 0;
    16641665                $args['hierarchical'] = false;
     
    16911692                }
    16921693        }
    16931694
     1695        // Check for heirarchical taxonomy
     1696        $children_only = $args['children_only']; // boolean
     1697        if ( $children_only ) {
     1698                // Empty if taxonomy is not heirarchical; term ids if it is.
     1699                // Note that we are not checking for a specific child or parent, just the
     1700                //  existence of an heirarchical taxonomy
     1701                $hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
     1702                if ( empty( $hierarchy )) {
     1703                        return $empty_array;
     1704                }
     1705        }
     1706
    16941707        // $args can be whatever, only use the args defined in defaults to compute the key
    16951708        $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
    16961709        $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
     
    18521865        $offset = $args['offset'];
    18531866
    18541867        // don't limit the query results when we have to descend the family tree
    1855         if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
     1868        if ( $number && ! $hierarchical && ! $child_of && '' === $parent && '' === $children_only) {
    18561869                if ( $offset ) {
    18571870                        $limits = 'LIMIT ' . $offset . ',' . $number;
    18581871                } else {
     
    19501963                }
    19511964        }
    19521965
     1966        if ( $children_only ) {
     1967                $terms = array();
     1968                $term_heirarchy = _get_term_hierarchy( reset( $taxonomies ) );
     1969                if ( ! empty( $term_heirarchy ) ) {
     1970                        // Get all terms, then purge the non-child ones by testing for further child terms
     1971                        $all_terms = get_terms($taxonomies, array('hide_empty' => $args['hide_empty']));
     1972                        foreach ( $all_terms as $term ) {
     1973                                if ( ! $term_heirarchy[$term->term_id] ) {
     1974                                        array_push( $terms, $term );
     1975                                }
     1976                        }
     1977                }
     1978        }
     1979
    19531980        // Update term counts to include children.
    19541981        if ( $args['pad_counts'] && 'all' == $_fields ) {
    19551982                _pad_term_counts( $terms, reset( $taxonomies ) );