Definition:
function &get_terms($taxonomies, $args = '') {}
Retrieve the terms in a given taxonomy or list of taxonomies.
You can fully inject any customizations to the query before it is sent, as well as control the output with a filter.
Parameters
- string|array $taxonomies: Taxonomy name or list of Taxonomy names
- string|array $args: The values of what to search for when returning terms
Return values
returns:List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.
Defined filters
- get_terms_args
apply_filters( 'get_terms_args', $args, $taxonomies ) - get_terms
apply_filters('get_terms', $cache, $taxonomies, $args) - get_terms_orderby
apply_filters( 'get_terms_orderby', $orderby, $args ) - list_terms_exclusions
apply_filters('list_terms_exclusions', $exclusions, $args ) - get_terms_fields
apply_filters( 'get_terms_fields', $selects, $args ) - terms_clauses
apply_filters( 'terms_clauses', compact( $pieces ) - get_terms
apply_filters('get_terms', array() - get_terms
apply_filters('get_terms', $terms, $taxonomies, $args)
Source code
function &get_terms($taxonomies, $args = '') {
global $wpdb;
$empty_array = array();
$single_taxonomy = false;
if ( !is_array($taxonomies) ) {
$single_taxonomy = true;
$taxonomies = array($taxonomies);
}
foreach ( $taxonomies as $taxonomy ) {
if ( ! taxonomy_exists($taxonomy) ) {
$error = new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
return $error;
}
}
$defaults = array('orderby' => 'name', 'order' => 'ASC',
'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '',
'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
$args = wp_parse_args( $args, $defaults );
$args['number'] = absint( $args['number'] );
$args['offset'] = absint( $args['offset'] );
if ( !$single_taxonomy || !is_taxonomy_hierarchical($taxonomies[0]) ||
'' !== $args['parent'] ) {
$args['child_of'] = 0;
$args['hierarchical'] = false;
$args['pad_counts'] = false;
}
if ( 'all' == $args['get'] ) {
$args['child_of'] = 0;
$args['hide_empty'] = 0;
$args['hierarchical'] = false;
$args['pad_counts'] = false;
}
$args = apply_filters( 'get_terms_args', $args, $taxonomies );
extract($args, EXTR_SKIP);
if ( $child_of ) {
$hierarchy = _get_term_hierarchy($taxonomies[0]);
if ( !isset($hierarchy[$child_of]) )
return $empty_array;
}
if ( $parent ) {
$hierarchy = _get_term_hierarchy($taxonomies[0]);
if ( !isset($hierarchy[$parent]) )
return $empty_array;
}
// $args can be whatever, only use the args defined in defaults to compute the key
$filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
$key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
$last_changed = wp_cache_get('last_changed', 'terms');
if ( !$last_changed ) {
$last_changed = time();
wp_cache_set('last_changed', $last_changed, 'terms');
}
$cache_key = "get_terms:$key:$last_changed";
$cache = wp_cache_get( $cache_key, 'terms' );
if ( false !== $cache ) {
$cache = apply_filters('get_terms', $cache, $taxonomies, $args);
return $cache;
}
$_orderby = strtolower($orderby);
if ( 'count' == $_orderby )
$orderby = 'tt.count';
else if ( 'name' == $_orderby )
$orderby = 't.name';
else if ( 'slug' == $_orderby )
$orderby = 't.slug';
else if ( 'term_group' == $_orderby )
$orderby = 't.term_group';
else if ( 'none' == $_orderby )
$orderby = '';
elseif ( empty($_orderby) || 'id' == $_orderby )
$orderby = 't.term_id';
else
$orderby = 't.name';
$orderby = apply_filters( 'get_terms_orderby', $orderby, $args );
if ( !empty($orderby) )
$orderby = "ORDER BY $orderby";
else
$order = '';
$order = strtoupper( $order );
if ( '' !== $order && !in_array( $order, array( 'ASC', 'DESC' ) ) )
$order = 'ASC';
$where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
$inclusions = '';
if ( !empty($include) ) {
$exclude = '';
$exclude_tree = '';
$interms = wp_parse_id_list($include);
foreach ( $interms as $interm ) {
if ( empty($inclusions) )
$inclusions = ' AND ( t.term_id = ' . intval($interm) . ' ';
else
$inclusions .= ' OR t.term_id = ' . intval($interm) . ' ';
}
}
if ( !empty($inclusions) )
$inclusions .= ')';
$where .= $inclusions;
$exclusions = '';
if ( !empty( $exclude_tree ) ) {
$excluded_trunks = wp_parse_id_list($exclude_tree);
foreach ( $excluded_trunks as $extrunk ) {
$excluded_children = (array) get_terms($taxonomies[0], array('child_of' => intval($extrunk), 'fields' => 'ids', 'hide_empty' => 0));
$excluded_children[] = $extrunk;
foreach( $excluded_children as $exterm ) {
if ( empty($exclusions) )
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
else
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
}
}
}
if ( !empty($exclude) ) {
$exterms = wp_parse_id_list($exclude);
foreach ( $exterms as $exterm ) {
if ( empty($exclusions) )
$exclusions = ' AND ( t.term_id <> ' . intval($exterm) . ' ';
else
$exclusions .= ' AND t.term_id <> ' . intval($exterm) . ' ';
}
}
if ( !empty($exclusions) )
$exclusions .= ')';
$exclusions = apply_filters('list_terms_exclusions', $exclusions, $args );
$where .= $exclusions;
if ( !empty($slug) ) {
$slug = sanitize_title($slug);
$where .= " AND t.slug = '$slug'";
}
if ( !empty($name__like) ) {
$name__like = like_escape( $name__like );
$where .= $wpdb->prepare( " AND t.name LIKE %s", $name__like . '%' );
}
if ( '' !== $parent ) {
$parent = (int) $parent;
$where .= " AND tt.parent = '$parent'";
}
if ( $hide_empty && !$hierarchical )
$where .= ' AND tt.count > 0';
// don't limit the query results when we have to descend the family tree
if ( ! empty($number) && ! $hierarchical && empty( $child_of ) && '' === $parent ) {
if ( $offset )
$limits = 'LIMIT ' . $offset . ',' . $number;
else
$limits = 'LIMIT ' . $number;
} else {
$limits = '';
}
if ( !empty($search) ) {
$search = like_escape($search);
$where .= $wpdb->prepare( " AND (t.name LIKE %s)", '%' . $search . '%');
}
$selects = array();
switch ( $fields ) {
case 'all':
$selects = array('t.*', 'tt.*');
break;
case 'ids':
case 'id=>parent':
$selects = array('t.term_id', 'tt.parent', 'tt.count');
break;
case 'names':
$selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name');
break;
case 'count':
$orderby = '';
$order = '';
$selects = array('COUNT(*)');
}
$_fields = $fields;
$fields = implode(', ', apply_filters( 'get_terms_fields', $selects, $args ));
$join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
$pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
$clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
foreach ( $pieces as $piece )
$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
$query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
$fields = $_fields;
if ( 'count' == $fields ) {
$term_count = $wpdb->get_var($query);
return $term_count;
}
$terms = $wpdb->get_results($query);
if ( 'all' == $fields ) {
update_term_cache($terms);
}
if ( empty($terms) ) {
wp_cache_add( $cache_key, array(), 'terms', 86400 ); // one day
$terms = apply_filters('get_terms', array(), $taxonomies, $args);
return $terms;
}
if ( $child_of ) {
$children = _get_term_hierarchy($taxonomies[0]);
if ( ! empty($children) )
$terms = & _get_term_children($child_of, $terms, $taxonomies[0]);
}
// Update term counts to include children.
if ( $pad_counts && 'all' == $fields )
_pad_term_counts($terms, $taxonomies[0]);
// Make sure we show empty categories that have children.
if ( $hierarchical && $hide_empty && is_array($terms) ) {
foreach ( $terms as $k => $term ) {
if ( ! $term->count ) {
$children = _get_term_children($term->term_id, $terms, $taxonomies[0]);
if ( is_array($children) )
foreach ( $children as $child )
if ( $child->count )
continue 2;
// It really is empty
unset($terms[$k]);
}
}
}
reset ( $terms );
$_terms = array();
if ( 'id=>parent' == $fields ) {
while ( $term = array_shift($terms) )
$_terms[$term->term_id] = $term->parent;
$terms = $_terms;
} elseif ( 'ids' == $fields ) {
while ( $term = array_shift($terms) )
$_terms[] = $term->term_id;
$terms = $_terms;
} elseif ( 'names' == $fields ) {
while ( $term = array_shift($terms) )
$_terms[] = $term->name;
$terms = $_terms;
}
if ( 0 < $number && intval(@count($terms)) > $number ) {
$terms = array_slice($terms, $offset, $number);
}
wp_cache_add( $cache_key, $terms, 'terms', 86400 ); // one day
$terms = apply_filters('get_terms', $terms, $taxonomies, $args);
return $terms;
}
1749

February 12, 2011 


No comments yet... Be the first to leave a reply!