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

git-svn-id: http://svn.automattic.com/wordpress/trunk@6808 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-02-13 09:30:26 +00:00
parent 8d1af802b2
commit c21c9af12a
4 changed files with 26 additions and 27 deletions

View File

@ -45,18 +45,18 @@ printf( _c( '%1$s%2$s%3$s|You can reorder these: 1: Pages, 2: by {s}, 3: matchin
$avail_post_stati = get_available_post_statuses('page'); $avail_post_stati = get_available_post_statuses('page');
$status_links = array(); $status_links = array();
$num_posts = wp_count_posts('page');
foreach ( $post_stati as $status => $label ) { foreach ( $post_stati as $status => $label ) {
$class = ''; $class = '';
if ( !in_array($status, $avail_post_stati) ) if ( !in_array($status, $avail_post_stati) )
continue; continue;
$num_posts = wp_count_posts('page', $status);
if ( $status == $_GET['post_status'] ) if ( $status == $_GET['post_status'] )
$class = ' class="current"'; $class = ' class="current"';
$status_links[] = "<li><a href=\"edit-pages.php?post_status=$status\"$class>" . $status_links[] = "<li><a href=\"edit-pages.php?post_status=$status\"$class>" .
sprintf($label[2], $num_posts) . '</a>'; sprintf($label[2], $num_posts->$status) . '</a>';
} }
$class = empty($_GET['post_status']) ? ' class="current"' : ''; $class = empty($_GET['post_status']) ? ' class="current"' : '';
$status_links[] = "<li><a href=\"edit-pages.php\"$class>All Pages</a>"; $status_links[] = "<li><a href=\"edit-pages.php\"$class>All Pages</a>";

View File

@ -52,18 +52,18 @@ if ( is_single() ) {
<ul class="subsubsub"> <ul class="subsubsub">
<?php <?php
$status_links = array(); $status_links = array();
$num_posts = wp_count_posts('post');
foreach ( $post_stati as $status => $label ) { foreach ( $post_stati as $status => $label ) {
$class = ''; $class = '';
if ( !in_array($status, $avail_post_stati) ) if ( !in_array($status, $avail_post_stati) )
continue; continue;
$num_posts = wp_count_posts('post', $status);
if ( $status == $_GET['post_status'] ) if ( $status == $_GET['post_status'] )
$class = ' class="current"'; $class = ' class="current"';
$status_links[] = "<li><a href=\"edit.php?post_status=$status\"$class>" . $status_links[] = "<li><a href=\"edit.php?post_status=$status\"$class>" .
sprintf($label[2], $num_posts) . '</a>'; sprintf($label[2], $num_posts->$status) . '</a>';
} }
$class = empty($_GET['post_status']) ? ' class="current"' : ''; $class = empty($_GET['post_status']) ? ' class="current"' : '';
$status_links[] = "<li><a href=\"edit.php\"$class>All Posts</a>"; $status_links[] = "<li><a href=\"edit.php\"$class>All Posts</a>";

View File

@ -37,15 +37,8 @@ $today = current_time('mysql', 1);
<h3 class="reallynow"><?php _e('Right Now'); ?> <a href="post-new.php" class="rbutton"><?php _e('Write a New Post'); ?></a> <a href="page-new.php" class="rbutton"><?php _e('Write a New Page'); ?></a></h3> <h3 class="reallynow"><?php _e('Right Now'); ?> <a href="post-new.php" class="rbutton"><?php _e('Write a New Post'); ?></a> <a href="page-new.php" class="rbutton"><?php _e('Write a New Page'); ?></a></h3>
<?php <?php
$num_posts = wp_count_posts('post', 'publish'); $num_posts = wp_count_posts( 'post' );
$num_pages = wp_count_posts( 'page' );
$num_pages = wp_count_posts('page', 'publish');
$num_drafts = wp_count_posts('post', 'draft');
$num_future = wp_count_posts('post', 'future');
$num_comments = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1'");
$num_cats = wp_count_terms('category'); $num_cats = wp_count_terms('category');
@ -53,17 +46,17 @@ $num_tags = wp_count_terms('post_tag');
$post_type_texts = array(); $post_type_texts = array();
if ( $num_posts ) { if ( !empty($num_posts->publish) ) {
$post_type_texts[] = '<a href="edit.php">'.sprintf( __ngettext( '%s post', '%s posts', $num_posts ), number_format_i18n( $num_posts ) ).'</a>'; $post_type_texts[] = '<a href="edit.php">'.sprintf( __ngettext( '%s post', '%s posts', $num_posts->publish ), number_format_i18n( $num_posts->publish ) ).'</a>';
} }
if ( $num_pages ) { if ( !empty($num_pages->publish) ) {
$post_type_texts[] = '<a href="edit-pages.php">'.sprintf( __ngettext( '%s page', '%s pages', $num_pages ), number_format_i18n( $num_pages ) ).'</a>'; $post_type_texts[] = '<a href="edit-pages.php">'.sprintf( __ngettext( '%s page', '%s pages', $num_pages->publish ), number_format_i18n( $num_pages->publish ) ).'</a>';
} }
if ( $num_drafts ) { if ( !empty($num_posts->draft) ) {
$post_type_texts[] = '<a href="edit.php?post_status=draft">'.sprintf( __ngettext( '%s draft', '%s drafts', $num_drafts ), number_format_i18n( $num_drafts ) ).'</a>'; $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>';
} }
if ( $num_future ) { if ( !empty($num_posts->future) ) {
$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>'; $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>';
} }
$cats_text = '<a href="categories.php">'.sprintf( __ngettext( '%s category', '%s categories', $num_cats ), number_format_i18n( $num_cats ) ).'</a>'; $cats_text = '<a href="categories.php">'.sprintf( __ngettext( '%s category', '%s categories', $num_cats ), number_format_i18n( $num_cats ) ).'</a>';

View File

@ -782,7 +782,7 @@ function sanitize_post_field($field, $value, $post_id, $context) {
} }
/** /**
* wp_count_posts() - Count number of posts with a given type and status * wp_count_posts() - Count number of posts with a given type
* *
* {@internal Missing Long Description}} * {@internal Missing Long Description}}
* *
@ -791,13 +791,19 @@ function sanitize_post_field($field, $value, $post_id, $context) {
* @since 2.5 * @since 2.5
* *
* @param string $type Post type * @param string $type Post type
* @param string $status Post status * @return array Number of posts for each status
* @return int Number of posts
*/ */
function wp_count_posts( $type = 'post', $status = 'publish' ) { function wp_count_posts( $type = 'post' ) {
global $wpdb; global $wpdb;
return $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_type = %s AND post_status = %s", $type, $status) ); $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 );
$stats = array( );
foreach( (array) $count as $row_num => $row ) {
$stats[$row['post_status']] = $row['num_posts'];
}
return (object) $stats;
} }
/** /**