Use get_terms() in wp_count_terms(). Props scribu. Fixes #10746

git-svn-id: http://svn.automattic.com/wordpress/trunk@13491 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
dd32 2010-02-28 07:37:24 +00:00
parent ac9c531035
commit 1b564b41f5
2 changed files with 33 additions and 26 deletions

View File

@ -230,20 +230,14 @@ if ( 'post_tag' == $taxonomy ) {
$tags_per_page = apply_filters( 'edit_' . $taxonomy . '_per_page', $tags_per_page ); $tags_per_page = apply_filters( 'edit_' . $taxonomy . '_per_page', $tags_per_page );
} }
if ( !empty($_GET['s']) ) { $searchterms = !empty($_GET['s']) ? trim(stripslashes($_GET['s'])) : '';
$searchterms = trim(stripslashes($_GET['s']));
$total_terms = count( get_terms( $taxonomy, array( 'search' => $searchterms, 'number' => 0, 'hide_empty' => 0 ) ) );
} else {
$searchterms = '';
$total_terms = wp_count_terms($taxonomy);
}
$page_links = paginate_links( array( $page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ), 'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '', 'format' => '',
'prev_text' => __('«'), 'prev_text' => __('«'),
'next_text' => __('»'), 'next_text' => __('»'),
'total' => ceil($total_terms / $tags_per_page), 'total' => ceil(wp_count_terms($taxonomy, array('search' => $searchterms)) / $tags_per_page),
'current' => $pagenum 'current' => $pagenum
)); ));

View File

@ -871,16 +871,29 @@ function &get_terms($taxonomies, $args = '') {
} }
$selects = array(); $selects = array();
if ( 'all' == $fields ) switch ( $fields ) {
$selects = array('t.*', 'tt.*'); case 'all':
else if ( 'ids' == $fields || 'id=>parent' == $fields ) $selects = array('t.*', 'tt.*');
$selects = array('t.term_id', 'tt.parent', 'tt.count'); break;
else if ( 'names' == $fields ) case 'ids':
$selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name'); 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':
$selects = array('COUNT(*)');
}
$select_this = implode(', ', apply_filters( 'get_terms_fields', $selects, $args )); $select_this = implode(', ', apply_filters( 'get_terms_fields', $selects, $args ));
$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 $limit"; $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 $limit";
if ( 'count' == $fields ) {
$term_count = $wpdb->get_var($query);
return $term_count;
}
$terms = $wpdb->get_results($query); $terms = $wpdb->get_results($query);
if ( 'all' == $fields ) { if ( 'all' == $fields ) {
update_term_cache($terms); update_term_cache($terms);
@ -1130,32 +1143,32 @@ function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
/** /**
* Count how many terms are in Taxonomy. * Count how many terms are in Taxonomy.
* *
* Default $args is 'ignore_empty' which can be <code>'ignore_empty=true'</code> * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true).
* or <code>array('ignore_empty' => true);</code>.
* *
* @package WordPress * @package WordPress
* @subpackage Taxonomy * @subpackage Taxonomy
* @since 2.3.0 * @since 2.3.0
* *
* @uses $wpdb * @uses get_terms()
* @uses wp_parse_args() Turns strings into arrays and merges defaults into an array. * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array.
* *
* @param string $taxonomy Taxonomy name * @param string $taxonomy Taxonomy name
* @param array|string $args Overwrite defaults * @param array|string $args Overwrite defaults. See get_terms()
* @return int How many terms are in $taxonomy * @return int How many terms are in $taxonomy
*/ */
function wp_count_terms( $taxonomy, $args = array() ) { function wp_count_terms( $taxonomy, $args = array() ) {
global $wpdb; $defaults = array('hide_empty' => false);
$defaults = array('ignore_empty' => false);
$args = wp_parse_args($args, $defaults); $args = wp_parse_args($args, $defaults);
extract($args, EXTR_SKIP);
$where = ''; // backwards compatibility
if ( $ignore_empty ) if ( isset($args['ignore_empty']) ) {
$where = 'AND count > 0'; $args['hide_empty'] = $args['ignore_empty'];
unset($args['ignore_empty']);
}
return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = %s $where", $taxonomy) ); $args['fields'] = 'count';
return get_terms($taxonomy, $args);
} }
/** /**