Make WordPress Core

Changeset 7775


Ignore:
Timestamp:
04/22/2008 09:26:01 PM (18 years ago)
Author:
ryan
Message:

Consolidate get_pending_comments_num() queries. see #6770

Location:
trunk/wp-admin
Files:
2 edited

Legend:

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

    r7640 r7775  
    2323$bgcolor = '';
    2424add_filter('the_title','wp_specialchars');
     25
     26// Create array of post IDs.
     27$post_ids = array();
     28foreach ( $wp_query->posts as $a_post )
     29    $post_ids[] = $a_post->ID;
     30
     31$comment_pending_count = get_pending_comments_num($post_ids);
     32
    2533while (have_posts()) : the_post();
    2634$class = 'alternate' == $class ? '' : 'alternate';
     
    114122        <td class="num"><div class="post-com-count-wrapper">
    115123        <?php
    116         $left = get_pending_comments_num( $post->ID );
     124        $left = isset($comment_pending_count) ? $comment_pending_count[$post->ID] : 0;
    117125        $pending_phrase = sprintf( __('%s pending'), number_format( $left ) );
    118126        if ( $left )
  • trunk/wp-admin/includes/comment.php

    r7645 r7775  
    6767function get_pending_comments_num( $post_id ) {
    6868    global $wpdb;
    69     $post_id = (int) $post_id;
    70     $pending = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '0'", $post_id) );
    71     return $pending;
     69
     70    $single = false;
     71    if ( !is_array($post_id) ) {
     72        $post_id = (array) $post_id;
     73        $single = true;
     74    }
     75    $post_id = array_map('intval', $post_id);
     76    $post_id = "'" . implode("', '", $post_id) . "'";
     77
     78    $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 );
     79
     80    if ( empty($pending) )
     81        return 0;
     82
     83    if ( $single )
     84        return $pending[0][1];
     85
     86    $pending_keyed = array();
     87    foreach ( $pending as $pend )
     88        $pending_keyed[$pend[0]] = $pend[1];
     89
     90    return $pending_keyed;
    7291}
    7392
Note: See TracChangeset for help on using the changeset viewer.