Consolidate get_pending_comments_num() queries. see #6770

git-svn-id: http://svn.automattic.com/wordpress/trunk@7775 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-04-22 21:26:01 +00:00
parent bf40169710
commit 444391eaff
2 changed files with 31 additions and 4 deletions

View File

@ -22,6 +22,14 @@
if ( have_posts() ) { if ( have_posts() ) {
$bgcolor = ''; $bgcolor = '';
add_filter('the_title','wp_specialchars'); add_filter('the_title','wp_specialchars');
// Create array of post IDs.
$post_ids = array();
foreach ( $wp_query->posts as $a_post )
$post_ids[] = $a_post->ID;
$comment_pending_count = get_pending_comments_num($post_ids);
while (have_posts()) : the_post(); while (have_posts()) : the_post();
$class = 'alternate' == $class ? '' : 'alternate'; $class = 'alternate' == $class ? '' : 'alternate';
global $current_user; global $current_user;
@ -113,7 +121,7 @@ foreach($posts_columns as $column_name=>$column_display_name) {
?> ?>
<td class="num"><div class="post-com-count-wrapper"> <td class="num"><div class="post-com-count-wrapper">
<?php <?php
$left = get_pending_comments_num( $post->ID ); $left = isset($comment_pending_count) ? $comment_pending_count[$post->ID] : 0;
$pending_phrase = sprintf( __('%s pending'), number_format( $left ) ); $pending_phrase = sprintf( __('%s pending'), number_format( $left ) );
if ( $left ) if ( $left )
echo '<strong>'; echo '<strong>';

View File

@ -66,9 +66,28 @@ function get_comment_to_edit( $id ) {
function get_pending_comments_num( $post_id ) { function get_pending_comments_num( $post_id ) {
global $wpdb; global $wpdb;
$post_id = (int) $post_id;
$pending = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '0'", $post_id) ); $single = false;
return $pending; if ( !is_array($post_id) ) {
$post_id = (array) $post_id;
$single = true;
}
$post_id = array_map('intval', $post_id);
$post_id = "'" . implode("', '", $post_id) . "'";
$pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id ) AND comment_approved = '0' GROUP BY comment_post_ID", ARRAY_N );
if ( empty($pending) )
return 0;
if ( $single )
return $pending[0][1];
$pending_keyed = array();
foreach ( $pending as $pend )
$pending_keyed[$pend[0]] = $pend[1];
return $pending_keyed;
} }
// Add avatars to relevant places in admin, or try to // Add avatars to relevant places in admin, or try to