Created
January 3, 2013 05:23
-
-
Save lgedeon/4441042 to your computer and use it in GitHub Desktop.
Simplified get terms - had to do this to get to something else but thought it might come in handy someday
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get_terms($taxonomy, $args = '') { | |
global $wpdb; | |
$empty_array = array(); | |
$single_taxonomy = true; | |
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 ( !is_taxonomy_hierarchical($taxonomy) || | |
'' !== $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; | |
} | |
extract($args, EXTR_SKIP); | |
$orderby = ''; | |
$order = ''; | |
$where = "tt.taxonomy IN ('" . $taxonomy . "')"; | |
$inclusions = ''; | |
$exclusions = ''; | |
$limits = ''; | |
$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( ', ', $selects ); | |
$join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; | |
$query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits"; | |
$fields = $_fields; | |
$terms = $wpdb->get_results($query); | |
// Update term counts to include children. | |
if ( $pad_counts && 'all' == $fields ) | |
_pad_term_counts($terms, $taxonomy); | |
// Make sure we show empty categories that have children. | |
if ( true ) { | |
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 ); | |
return $terms; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment