Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




get_comment_count [ WordPress Function ]

get_comment_count ( $post_id = 0 )
Parameters:
  • (int) $post_id Optional. Comment amount in post if > 0, else total comments blog wide.
Uses:
  • $wpdb
Returns:
  • (array) The amount of spam, approved, awaiting moderation, and total comments.
Defined at:



The amount of comments in a post or total comments.

A lot like {@link wp_count_comments()}, in that they both return comment stats (albeit with different types). The {@link wp_count_comments()} actual caches, but this function does not.

Source


<?php
function get_comment_count$post_id ) {
    global 
$wpdb;

    
$post_id = (int) $post_id;

    
$where '';
    if ( 
$post_id ) {
        
$where $wpdb->prepare("WHERE comment_post_ID = %d"$post_id);
    }

    
$totals = (array) $wpdb->get_results("
        SELECT comment_approved, COUNT( * ) AS total
        FROM 
{$wpdb->comments}
        
{$where}
        GROUP BY comment_approved
    "
ARRAY_A);

    
$comment_count = array(
        
"approved"              => 0,
        
"awaiting_moderation"   => 0,
        
"spam"                  => 0,
        
"total_comments"        => 0
    
);

    foreach ( 
$totals as $row ) {
        switch ( 
$row['comment_approved'] ) {
            case 
'spam':
                
$comment_count['spam'] = $row['total'];
                
$comment_count["total_comments"] += $row['total'];
                break;
            case 
1:
                
$comment_count['approved'] = $row['total'];
                
$comment_count['total_comments'] += $row['total'];
                break;
            case 
0:
                
$comment_count['awaiting_moderation'] = $row['total'];
                
$comment_count['total_comments'] += $row['total'];
                break;
            default:
                break;
        }
    }

    return 
$comment_count;
}
?>

Examples [ wp-snippets.com ]

Google Arama Sonuçlarý

Dahasý ...

0 User Note(s)

Henüz yok. Ýlk sen ol!

Yeni Ekle ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics