Ticket #8087: get_terms.diff
File get_terms.diff, 7.2 KB (added by , 15 years ago) |
---|
-
wp-includes/taxonomy.php
505 505 } 506 506 507 507 /** 508 * Retrieve the terms in taxonomy or list of taxonomies.508 * Retrieve the terms in a given taxonomy or list of taxonomies. 509 509 * 510 510 * You can fully inject any customizations to the query before it is sent, as 511 511 * well as control the output with a filter. … … 518 518 * The 'list_terms_exclusions' filter passes the compiled exclusions along with 519 519 * the $args. 520 520 * 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: 522 522 * 523 523 * orderby - Default is 'name'. Can be name, count, or nothing (will use 524 524 * term_id). 525 525 * 526 526 * 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 527 * 533 * The argument 'pad_counts' will count all of the children along with the 534 * $terms. 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. 535 534 * 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. 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. 539 541 * 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. 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 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. 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. 548 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. 575 * 549 576 * @package WordPress 550 577 * @subpackage Taxonomy 551 578 * @since 2.3.0 … … 706 733 if ( 'all' == $fields ) 707 734 $select_this = 't.*, tt.*'; 708 735 else if ( 'ids' == $fields ) 709 $select_this = 't.term_id ';736 $select_this = 't.term_id, tt.parent, tt.count'; 710 737 else if ( 'names' == $fields ) 711 $select_this = 't. name';738 $select_this = 't.term_id, tt.parent, tt.count, t.name'; 712 739 713 740 $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"; 714 741 … … 716 743 $terms = $wpdb->get_results($query); 717 744 update_term_cache($terms); 718 745 } else if ( ('ids' == $fields) || ('names' == $fields) ) { 719 $terms = $wpdb->get_ col($query);746 $terms = $wpdb->get_results($query); 720 747 } 721 748 722 749 if ( empty($terms) ) { … … 726 753 return $terms; 727 754 } 728 755 729 if ( $child_of || $hierarchical) {756 if ( $child_of ) { 730 757 $children = _get_term_hierarchy($taxonomies[0]); 731 758 if ( ! empty($children) ) 732 759 $terms = & _get_term_children($child_of, $terms, $taxonomies[0]); 733 760 } 734 761 735 762 // Update term counts to include children. 736 if ( $pad_counts )763 if ( $pad_counts && 'all' == $fields ) 737 764 _pad_term_counts($terms, $taxonomies[0]); 738 765 739 766 // Make sure we show empty categories that have children. … … 752 779 } 753 780 } 754 781 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 } 755 793 756 794 wp_cache_add( $cache_key, $terms, 'terms' ); 757 795 … … 1889 1927 1890 1928 1891 1929 /** 1892 * Get array of child terms.1930 * Get the subset of $terms that are descendants of $term_id. 1893 1931 * 1894 * If $terms is an array of objects, then objects will returned from the 1895 * function. If $terms is an array of IDs, then an array of ids of children will 1896 * be returned. 1932 * If $terms is an array of objects, then _get_term_children returns an array of objects. 1933 * If $terms is an array of IDs, then _get_term_children returns an array of IDs. 1897 1934 * 1898 1935 * @package WordPress 1899 1936 * @subpackage Taxonomy 1900 1937 * @access private 1901 1938 * @since 2.3.0 1902 1939 * 1903 * @param int $term_id Look for this Term ID in $terms1904 * @param array $terms List of Term IDs1905 * @param string $taxonomy T erm Context1906 * @return array Empty if $terms is empty else returns full list of child terms.1940 * @param int $term_id The ancestor term: all returned terms should be descendants of $term_id. 1941 * @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. 1942 * @param string $taxonomy The taxonomy which determines the hierarchy of the terms. 1943 * @return array The subset of $terms that are descendants of $term_id. 1907 1944 */ 1908 1945 function &_get_term_children($term_id, $terms, $taxonomy) { 1909 1946 $empty_array = array();