WordPress.org

Make WordPress Core

Changeset 9652


Ignore:
Timestamp:
11/13/08 00:20:12 (6 years ago)
Author:
ryan
Message:

get_terms() improvements from filosofo. fixes #8087

File:
1 edited

Legend:

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

    r9602 r9652  
    506506 
    507507/** 
    508  * Retrieve the terms in taxonomy or list of taxonomies. 
     508 * Retrieve the terms in a given taxonomy or list of taxonomies. 
    509509 * 
    510510 * You can fully inject any customizations to the query before it is sent, as 
     
    519519 * the $args. 
    520520 * 
    521  * The list that $args can contain, which will overwrite the defaults. 
     521 * The list of arguments that $args can contain, which will overwrite the defaults: 
    522522 * 
    523523 * orderby - Default is 'name'. Can be name, count, or nothing (will use 
     
    525525 * 
    526526 * order - Default is ASC. Can use DESC. 
    527  * hide_empty - Default is true. Will not return empty $terms. 
    528  * fields - Default is all. 
    529  * slug - Any terms that has this value. Default is empty string. 
    530  * hierarchical - Whether to return hierarchical taxonomy. Default is true. 
    531  * name__like - Default is empty string. 
    532  * 
    533  * The argument 'pad_counts' will count all of the children along with the 
    534  * $terms. 
    535  * 
    536  * The 'get' argument allows for overwriting 'hide_empty' and 'child_of', which 
    537  * can be done by setting the value to 'all', instead of its default empty 
    538  * string value. 
    539  * 
    540  * The 'child_of' argument will be used if you use multiple taxonomy or the 
    541  * first $taxonomy isn't hierarchical or 'parent' isn't used. The default is 0, 
    542  * which will be translated to a false value. If 'child_of' is set, then 
    543  * 'child_of' value will be tested against $taxonomy to see if 'child_of' is 
    544  * contained within. Will return an empty array if test fails. 
    545  * 
    546  * If 'parent' is set, then it will be used to test against the first taxonomy. 
    547  * Much like 'child_of'. Will return an empty array if the test fails. 
     527 * 
     528 * hide_empty - Default is true. Will not return empty terms, which means 
     529 * terms whose count is 0 according to the given taxonomy. 
     530 *  
     531 * exclude - Default is an empty string.  A comma- or space-delimited string 
     532 * of term ids to exclude from the return array.  If 'include' is non-empty,  
     533 * 'exclude' is ignored. 
     534 * 
     535 * include - Default is an empty string.  A comma- or space-delimited string 
     536 * of term ids to include in the return array. 
     537 *  
     538 * number - The maximum number of terms to return.  Default is empty. 
     539 *  
     540 * offset - The number by which to offset the terms query. 
     541 * 
     542 * fields - Default is 'all', which returns an array of term objects.  
     543 * If 'fields' is 'ids' or 'names', returns an array of 
     544 * integers or strings, respectively. 
     545 * 
     546 * slug - Returns terms whose "slug" matches this value. Default is empty string. 
     547 *  
     548 * hierarchical - Whether to include terms that have non-empty descendants 
     549 * (even if 'hide_empty' is set to true). 
     550 *  
     551 * search - Returned terms' names will contain the value of 'search', 
     552 * case-insensitive.  Default is an empty string. 
     553 * 
     554 * name__like - Returned terms' names will begin with the value of 'name__like', 
     555 * case-insensitive. Default is empty string. 
     556 * 
     557 * The argument 'pad_counts', if set to true will include the quantity of a term's 
     558 * children in the quantity of each term's "count" object variable. 
     559 * 
     560 * The 'get' argument, if set to 'all' instead of its default empty string, 
     561 * returns terms regardless of ancestry or whether the terms are empty. 
     562 * 
     563 * The 'child_of' argument, when used, should be set to the integer of a term ID.  Its default 
     564 * is 0.  If set to a non-zero value, all returned terms will be descendants  
     565 * of that term according to the given taxonomy.  Hence 'child_of' is set to 0  
     566 * if more than one taxonomy is passed in $taxonomies, because multiple taxonomies  
     567 * make term ancestry ambiguous. 
     568 * 
     569 * The 'parent' argument, when used, should be set to the integer of a term ID.  Its default is 
     570 * the empty string '', which has a different meaning from the integer 0. 
     571 * If set to an integer value, all returned terms will have as an immediate  
     572 * ancestor the term whose ID is specified by that integer according to the given taxonomy. 
     573 * The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent' 
     574 * of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc. 
    548575 * 
    549576 * @package WordPress 
     
    707734        $select_this = 't.*, tt.*'; 
    708735    else if ( 'ids' == $fields ) 
    709         $select_this = 't.term_id'; 
     736        $select_this = 't.term_id, tt.parent, tt.count'; 
    710737    else if ( 'names' == $fields ) 
    711         $select_this = 't.name'; 
     738        $select_this = 't.term_id, tt.parent, tt.count, t.name'; 
    712739 
    713740    $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy IN ($in_taxonomies) $where ORDER BY $orderby $order $number"; 
     
    717744        update_term_cache($terms); 
    718745    } else if ( ('ids' == $fields) || ('names' == $fields) ) { 
    719         $terms = $wpdb->get_col($query); 
     746        $terms = $wpdb->get_results($query); 
    720747    } 
    721748 
     
    727754    } 
    728755 
    729     if ( $child_of || $hierarchical ) { 
     756    if ( $child_of ) { 
    730757        $children = _get_term_hierarchy($taxonomies[0]); 
    731758        if ( ! empty($children) ) 
     
    734761 
    735762    // Update term counts to include children. 
    736     if ( $pad_counts ) 
     763    if ( $pad_counts && 'all' == $fields ) 
    737764        _pad_term_counts($terms, $taxonomies[0]); 
    738765 
     
    753780    } 
    754781    reset ( $terms ); 
     782     
     783    $_terms = array(); 
     784    if ( 'ids' == $fields ) { 
     785        while ( $term = array_shift($terms) ) 
     786            $_terms[] = $term->term_id; 
     787        $terms = $_terms; 
     788    } elseif ( 'names' == $fields ) { 
     789        while ( $term = array_shift($terms) ) 
     790            $_terms[] = $term->name; 
     791        $terms = $_terms; 
     792    } 
    755793 
    756794    wp_cache_add( $cache_key, $terms, 'terms' ); 
     
    18951933 
    18961934/** 
    1897  * Get array of child terms. 
    1898  * 
    1899  * If $terms is an array of objects, then objects will returned from the 
    1900  * function. If $terms is an array of IDs, then an array of ids of children will 
    1901  * be returned. 
     1935 * Get the subset of $terms that are descendants of $term_id. 
     1936 * 
     1937 * If $terms is an array of objects, then _get_term_children returns an array of objects. 
     1938 * If $terms is an array of IDs, then _get_term_children returns an array of IDs. 
    19021939 * 
    19031940 * @package WordPress 
     
    19061943 * @since 2.3.0 
    19071944 * 
    1908  * @param int $term_id Look for this Term ID in $terms 
    1909  * @param array $terms List of Term IDs 
    1910  * @param string $taxonomy Term Context 
    1911  * @return array Empty if $terms is empty else returns full list of child terms. 
     1945 * @param int $term_id The ancestor term: all returned terms should be descendants of $term_id. 
     1946 * @param array $terms The set of terms---either an array of term objects or term IDs---from which those that are descendants of $term_id will be chosen. 
     1947 * @param string $taxonomy The taxonomy which determines the hierarchy of the terms. 
     1948 * @return array The subset of $terms that are descendants of $term_id.  
    19121949 */ 
    19131950function &_get_term_children($term_id, $terms, $taxonomy) { 
Note: See TracChangeset for help on using the changeset viewer.