Make WordPress Core

Changeset 13491


Ignore:
Timestamp:
02/28/2010 07:37:24 AM (17 years ago)
Author:
dd32
Message:

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

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/edit-tags.php

    r13450 r13491  
    231231}
    232232
    233 if ( !empty($_GET['s']) ) {
    234         $searchterms = trim(stripslashes($_GET['s']));
    235         $total_terms = count( get_terms( $taxonomy, array( 'search' => $searchterms, 'number' => 0, 'hide_empty' => 0 ) ) );
    236 } else {
    237         $searchterms = '';
    238         $total_terms = wp_count_terms($taxonomy);
    239 }
     233$searchterms = !empty($_GET['s']) ? trim(stripslashes($_GET['s'])) : '';
    240234
    241235$page_links = paginate_links( array(
    … …  
    244238        'prev_text' => __('«'),
    245239        'next_text' => __('»'),
    246         'total' => ceil($total_terms / $tags_per_page),
     240        'total' => ceil(wp_count_terms($taxonomy, array('search' => $searchterms)) / $tags_per_page),
    247241        'current' => $pagenum
    248242));
  • trunk/wp-includes/taxonomy.php

    r13430 r13491  
    872872
    873873        $selects = array();
    874         if ( 'all' == $fields )
    875                 $selects = array('t.*', 'tt.*');
    876         else if ( 'ids' == $fields || 'id=>parent' == $fields )
    877                 $selects = array('t.term_id', 'tt.parent', 'tt.count');
    878         else if ( 'names' == $fields )
    879                 $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name');
     874        switch ( $fields ) {
     875                case 'all':
     876                        $selects = array('t.*', 'tt.*');
     877                        break;
     878                case 'ids':
     879                case 'id=>parent':
     880                        $selects = array('t.term_id', 'tt.parent', 'tt.count');
     881                        break;
     882                case 'names':
     883                        $selects = array('t.term_id', 'tt.parent', 'tt.count', 't.name');
     884                        break;
     885                case 'count':
     886                        $selects = array('COUNT(*)');
     887        }
    880888    $select_this = implode(', ', apply_filters( 'get_terms_fields', $selects, $args ));
    881889
    882890        $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";
     891
     892        if ( 'count' == $fields ) {
     893                $term_count = $wpdb->get_var($query);
     894                return $term_count;
     895        }
    883896
    884897        $terms = $wpdb->get_results($query);
    … …  
    11311144 * Count how many terms are in Taxonomy.
    11321145 *
    1133  * Default $args is 'ignore_empty' which can be <code>'ignore_empty=true'</code>
    1134  * or <code>array('ignore_empty' => true);</code>.
    1135  *
    1136  * @package WordPress
    1137  * @subpackage Taxonomy
    1138  * @since 2.3.0
    1139  *
    1140  * @uses $wpdb
     1146 * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true).
     1147 *
     1148 * @package WordPress
     1149 * @subpackage Taxonomy
     1150 * @since 2.3.0
     1151 *
     1152 * @uses get_terms()
    11411153 * @uses wp_parse_args() Turns strings into arrays and merges defaults into an array.
    11421154 *
    11431155 * @param string $taxonomy Taxonomy name
    1144  * @param array|string $args Overwrite defaults
     1156 * @param array|string $args Overwrite defaults. See get_terms()
    11451157 * @return int How many terms are in $taxonomy
    11461158 */
    11471159function wp_count_terms( $taxonomy, $args = array() ) {
    1148         global $wpdb;
    1149 
    1150         $defaults = array('ignore_empty' => false);
     1160        $defaults = array('hide_empty' => false);
    11511161        $args = wp_parse_args($args, $defaults);
    1152         extract($args, EXTR_SKIP);
    1153 
    1154         $where = '';
    1155         if ( $ignore_empty )
    1156                 $where = 'AND count > 0';
    1157 
    1158         return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE taxonomy = %s $where", $taxonomy) );
     1162
     1163        // backwards compatibility
     1164        if ( isset($args['ignore_empty']) ) {
     1165                $args['hide_empty'] = $args['ignore_empty'];
     1166                unset($args['ignore_empty']);
     1167        }
     1168
     1169        $args['fields'] = 'count';
     1170
     1171        return get_terms($taxonomy, $args);
    11591172}
    11601173
Note: See TracChangeset for help on using the changeset viewer.