Changeset 7868 for trunk/wp-includes/comment.php
- Timestamp:
- 05/01/2008 04:52:48 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/comment.php
r7714 r7868 449 449 } 450 450 451 function wp_count_comments( ) {451 function wp_count_comments( $post_id = 0 ) { 452 452 global $wpdb; 453 453 454 $count = wp_cache_get('comments', 'counts'); 454 $post_id = (int) $post_id; 455 456 $count = wp_cache_get('comments', "counts-{$post_id}"); 455 457 456 458 if ( false !== $count ) 457 459 return $count; 458 460 459 $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} GROUP BY comment_approved", ARRAY_A ); 460 461 $where = ''; 462 if( $post_id > 0 ) 463 $where = $wpdb->prepare( "WHERE comment_post_ID = %d", $post_id ); 464 465 $count = $wpdb->get_results( "SELECT comment_approved, COUNT( * ) AS num_comments FROM {$wpdb->comments} {$where} GROUP BY comment_approved", ARRAY_A ); 466 467 $total = 0; 461 468 $stats = array( ); 462 469 $approved = array('0' => 'moderated', '1' => 'approved', 'spam' => 'spam'); 463 470 foreach( (array) $count as $row_num => $row ) { 471 $total += $row['num_comments']; 464 472 $stats[$approved[$row['comment_approved']]] = $row['num_comments']; 465 473 } 466 474 475 $stats['total_comments'] = $total; 467 476 foreach ( $approved as $key ) { 468 477 if ( empty($stats[$key]) ) … … 471 480 472 481 $stats = (object) $stats; 473 wp_cache_set('comments', $stats, 'counts');482 wp_cache_set('comments', $stats, "counts-{$post_id}"); 474 483 475 484 return $stats;
Note: See TracChangeset
for help on using the changeset viewer.