Changeset 6808

Show
Ignore:
Timestamp:
02/13/08 09:30:26 (9 months ago)
Author:
ryan
Message:

Reduce queries by wp_count_posts(). Props josephscott. fixes #5820

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-admin/edit-pages.php

    r6804 r6808  
    4646     
    4747$status_links = array(); 
     48$num_posts = wp_count_posts('page'); 
    4849foreach ( $post_stati as $status => $label ) { 
    4950    $class = ''; 
     
    5152    if ( !in_array($status, $avail_post_stati) ) 
    5253        continue; 
    53  
    54     $num_posts = wp_count_posts('page', $status); 
     54     
    5555    if ( $status == $_GET['post_status'] ) 
    5656        $class = ' class="current"'; 
    5757 
    5858    $status_links[] = "<li><a href=\"edit-pages.php?post_status=$status\"$class>" . 
    59     sprintf($label[2], $num_posts) . '</a>'; 
     59    sprintf($label[2], $num_posts->$status) . '</a>'; 
    6060} 
    6161$class = empty($_GET['post_status']) ? ' class="current"' : ''; 
  • trunk/wp-admin/edit.php

    r6807 r6808  
    5353<?php 
    5454$status_links = array(); 
     55$num_posts = wp_count_posts('post'); 
    5556foreach ( $post_stati as $status => $label ) { 
    5657    $class = ''; 
     
    5960        continue; 
    6061 
    61     $num_posts = wp_count_posts('post', $status); 
    6262    if ( $status == $_GET['post_status'] ) 
    6363        $class = ' class="current"'; 
    6464 
    6565    $status_links[] = "<li><a href=\"edit.php?post_status=$status\"$class>" . 
    66     sprintf($label[2], $num_posts) . '</a>'; 
     66    sprintf($label[2], $num_posts->$status) . '</a>'; 
    6767} 
    6868$class = empty($_GET['post_status']) ? ' class="current"' : ''; 
  • trunk/wp-admin/index.php

    r6766 r6808  
    3838 
    3939<?php 
    40 $num_posts = wp_count_posts('post', 'publish'); 
    41  
    42 $num_pages = wp_count_posts('page', 'publish'); 
    43  
    44 $num_drafts = wp_count_posts('post', 'draft'); 
    45  
    46 $num_future = wp_count_posts('post', 'future'); 
    47  
    48 $num_comments = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'"); 
     40$num_posts = wp_count_posts( 'post' ); 
     41$num_pages = wp_count_posts( 'page' ); 
    4942 
    5043$num_cats  = wp_count_terms('category'); 
     
    5447$post_type_texts = array(); 
    5548 
    56 if ( $num_posts ) { 
    57     $post_type_texts[] = '<a href="edit.php">'.sprintf( __ngettext( '%s post', '%s posts', $num_posts ), number_format_i18n( $num_posts ) ).'</a>'; 
     49if ( !empty($num_posts->publish) ) { 
     50    $post_type_texts[] = '<a href="edit.php">'.sprintf( __ngettext( '%s post', '%s posts', $num_posts->publish ), number_format_i18n( $num_posts->publish ) ).'</a>'; 
    5851} 
    59 if ( $num_pages ) { 
    60     $post_type_texts[] = '<a href="edit-pages.php">'.sprintf( __ngettext( '%s page', '%s pages', $num_pages ), number_format_i18n( $num_pages ) ).'</a>'; 
     52if ( !empty($num_pages->publish) ) { 
     53    $post_type_texts[] = '<a href="edit-pages.php">'.sprintf( __ngettext( '%s page', '%s pages', $num_pages->publish ), number_format_i18n( $num_pages->publish ) ).'</a>'; 
    6154} 
    62 if ( $num_drafts ) { 
    63     $post_type_texts[] = '<a href="edit.php?post_status=draft">'.sprintf( __ngettext( '%s draft', '%s drafts', $num_drafts ), number_format_i18n( $num_drafts ) ).'</a>'; 
     55if ( !empty($num_posts->draft) ) { 
     56    $post_type_texts[] = '<a href="edit.php?post_status=draft">'.sprintf( __ngettext( '%s draft', '%s drafts', $num_posts->draft ), number_format_i18n( $num_posts->draft ) ).'</a>'; 
    6457} 
    65 if ( $num_future ) { 
    66     $post_type_texts[] = '<a href="edit.php?post_status=future">'.sprintf( __ngettext( '%s scheduled post', '%s scheduled posts', $num_future ), number_format_i18n( $num_future ) ).'</a>'; 
     58if ( !empty($num_posts->future) ) { 
     59    $post_type_texts[] = '<a href="edit.php?post_status=future">'.sprintf( __ngettext( '%s scheduled post', '%s scheduled posts', $num_posts->future ), number_format_i18n( $num_posts->future ) ).'</a>'; 
    6760} 
    6861 
  • trunk/wp-includes/post.php

    r6803 r6808  
    783783 
    784784/** 
    785  * wp_count_posts() - Count number of posts with a given type and status 
     785 * wp_count_posts() - Count number of posts with a given type 
    786786 * 
    787787 * {@internal Missing Long Description}} 
     
    792792 * 
    793793 * @param string $type Post type 
    794  * @param string $status Post status 
    795  * @return int Number of posts 
    796  */ 
    797 function wp_count_posts( $type = 'post', $status = 'publish' ) { 
     794 * @return array Number of posts for each status 
     795 */ 
     796function wp_count_posts( $type = 'post' ) { 
    798797    global $wpdb; 
    799798 
    800     return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status = %s", $type, $status) ); 
     799    $count = $wpdb->get_results( $wpdb->prepare( "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s GROUP BY post_status", $type ), ARRAY_A ); 
     800 
     801    $stats = array( ); 
     802    foreach( (array) $count as $row_num => $row ) { 
     803        $stats[$row['post_status']] = $row['num_posts']; 
     804    } 
     805 
     806    return (object) $stats; 
    801807} 
    802808