From 1b564b41f55bd9c291346457e6802077eee6f8e4 Mon Sep 17 00:00:00 2001 From: dd32 Date: Sun, 28 Feb 2010 07:37:24 +0000 Subject: [PATCH] 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 --- wp-admin/edit-tags.php | 10 ++------ wp-includes/taxonomy.php | 49 +++++++++++++++++++++++++--------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/wp-admin/edit-tags.php b/wp-admin/edit-tags.php index 812475486..fef486025 100644 --- a/wp-admin/edit-tags.php +++ b/wp-admin/edit-tags.php @@ -230,20 +230,14 @@ if ( 'post_tag' == $taxonomy ) { $tags_per_page = apply_filters( 'edit_' . $taxonomy . '_per_page', $tags_per_page ); } -if ( !empty($_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); -} +$searchterms = !empty($_GET['s']) ? trim(stripslashes($_GET['s'])) : ''; $page_links = paginate_links( array( 'base' => add_query_arg( 'pagenum', '%#%' ), 'format' => '', 'prev_text' => __('«'), 'next_text' => __('»'), - 'total' => ceil($total_terms / $tags_per_page), + 'total' => ceil(wp_count_terms($taxonomy, array('search' => $searchterms)) / $tags_per_page), 'current' => $pagenum )); diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index f554226e6..39ca1c463 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -871,16 +871,29 @@ function &get_terms($taxonomies, $args = '') { } $selects = array(); - if ( 'all' == $fields ) - $selects = array('t.*', 'tt.*'); - else if ( 'ids' == $fields || 'id=>parent' == $fields ) - $selects = array('t.term_id', 'tt.parent', 'tt.count'); - else if ( 'names' == $fields ) - $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name'); + 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': + $selects = array('COUNT(*)'); + } $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"; + if ( 'count' == $fields ) { + $term_count = $wpdb->get_var($query); + return $term_count; + } + $terms = $wpdb->get_results($query); if ( 'all' == $fields ) { 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. * - * Default $args is 'ignore_empty' which can be 'ignore_empty=true' - * or array('ignore_empty' => true);. + * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true). * * @package WordPress * @subpackage Taxonomy * @since 2.3.0 * - * @uses $wpdb + * @uses get_terms() * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array. * * @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 */ function wp_count_terms( $taxonomy, $args = array() ) { - global $wpdb; - - $defaults = array('ignore_empty' => false); + $defaults = array('hide_empty' => false); $args = wp_parse_args($args, $defaults); - extract($args, EXTR_SKIP); - $where = ''; - if ( $ignore_empty ) - $where = 'AND count > 0'; + // backwards compatibility + if ( isset($args['ignore_empty']) ) { + $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); } /**