WordPress Get Total Comment Count

WordPress Comment Count

WordPress has builtin function wp_count_comments to count total comments for a given post id or all comments. The function will return totals of moderated, approved, spam, trash, post-trashed and total_comments. But we can’t use the function for special case like if we want to result total comments for single and multiple category or tag. We should build our own database queries to get the results. Below are code snippets collection to get the total post or site comments and some special cases.

[toc float=”none”]

Get Total Comments from Post ID

This code will display total comments from a post ID (example: 87).

$comments = wp_count_comments( 87 );
echo 'Comments for post 87<br />';
echo 'Comments in moderation: ' . $comments->moderated . '<br />'; 
echo 'Comments approved: ' . $comments->approved . '<br />';
echo 'Comments in Spam: ' . $comments->spam . '<br />';
echo 'Comments in Trash: ' . $comments->trash . '<br />';
echo 'Total Comments: ' . $comments->total_comments . '<br />';

Get Site Total Comments

This will display total comments from current site in single installation or multi-install (multisite).

$comments = wp_count_comments();
echo 'Comments for site <br />';
echo 'Comments in moderation: ' . $comments->moderated . '<br />'; 
echo 'Comments approved: ' . $comments->approved . '<br />';
echo 'Comments in Spam: ' . $comments->spam . '<br />';
echo 'Comments in Trash: ' . $comments->trash . '<br />';
echo 'Total Comments: ' . $comments->total_comments . '<br />';

Get Total Comments in Multisite

We can use the code above from current blog, but if we want to count the comments from other blog id (example: 2), we need to supply the table prefix for it.

global $wpdb;
$blog_prefix = $wpdb->get_blog_prefix(2);
$count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$blog_prefix}comments GROUP BY comment_approved", ARRAY_A );

Example result using $wpdb query:

print_r( $count );
array(
    [0] => array(
        [comment_approved] => 1
        [num_comments] => 5
    )
    [1] => array(
        [comment_approved] => spam
        [num_comments] => 1
    )
    [2] => array(
        [comment_approved] => trash
        [num_comments] => 1
    )
)

Get Total Comments from Comment Author

There are four table column for comment author details, comment_author for comment author name, comment_author_email, comment_author_url and comment_author_IP. We can use the column in the query search. The code below will count the total comments for an authors.

global $wpdb;
$comment_author = 'John Doe';
$where = $wpdb->prepare( "WHERE comment_author = %s", $comment_author );
$count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$wpdb->comments} $where GROUP BY comment_approved", ARRAY_A );

Get Total Comments from a Specific Date

global $wpdb;
$where = $wpdb->prepare( "
    WHERE YEAR(comment_date) = '%d' 
    AND MONTH(comment_date) = '%d' 
    AND DAYOFMONTH(comment_date) = '%d'", 
    2013, 10, 29
);
$count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$wpdb->comments} $where GROUP BY comment_approved", ARRAY_A );

or

$where = $wpdb->prepare( "WHERE DATE(comment_date) = '%s'", '2013-10-29' );

Get Total Comments from Dates Between

global $wpdb;
$where = $wpdb->prepare( 
    "WHERE comment_date BETWEEN '%s' AND '%s'", 
    '2013-10-15', 
    '2013-10-30' 
);
$count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$wpdb->comments} $where GROUP BY comment_approved", ARRAY_A );

Get Total Comments from Dates Range

global $wpdb;
$where = $wpdb->prepare(
    "WHERE DATE(comment_date) >= '%s' AND DATE(comment_date) <= '%s'",
    '2013-10-22',
    '2013-10-27'
);
$count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$wpdb->comments} $where GROUP BY comment_approved", ARRAY_A );

Get Total Comments from Current Date Time

For the current date:

global $wpdb;
$today = date("Y-m-d");
$where = $wpdb->prepare( 
    "WHERE DATE(comment_date) = '%s'",
    $today
);

For the current month:

$today = getdate();
$where = $wpdb->prepare( "
    WHERE YEAR(comment_date) ='%d'
    AND MONTH(comment_date) = '%d'",
    $today["year"],
    $today["mon"]
);

For the current year:

$today = getdate();
$where = $wpdb->prepare( "
    WHERE YEAR(comment_date) ='%d'
    $today["year"]
);

Get the results:

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

Get Total Comments from a Comment Parent

global $wpdb;
$where = $wpdb->prepare( "WHERE comment_parent = '%d'", 4);
$count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$wpdb->comments} $where GROUP BY comment_approved", ARRAY_A );

Get Total Comments from User ID

global $wpdb;
$where = $wpdb->prepare( "WHERE user_id = '%d'", 1);
$count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$wpdb->comments} $where GROUP BY comment_approved", ARRAY_A );

Get Total Comments from Term ID

Code below will get the post IDs from categories, tags or other custom taxonomy terms ID. We will then create comma separated post IDs.

global $wpdb;
$ids = array();
$posts = get_posts( array('category' => '9,11' ) );
foreach( $posts as $post )
    $ids[] = $post->ID;

$ids = implode( ",", $ids );
$count = $wpdb->get_results( "
    SELECT comment_approved, COUNT(*) AS num_comments 
    FROM {$wpdb->comments} WHERE comment_post_ID in ($ids) 
    GROUP BY comment_approved", ARRAY_A 
);

7 comments on “WordPress Get Total Comment Count

  1. Thanks for this, it really helped me get started with a comments count for my categories.

    Here are a few issues I found in the wild.

    1# Firstly get_posts only seem to return 5 posts as default, so you need to manually up the posts_per_page.

    2# $count returns a multidimensional array:

    comment_approved num_comments
    0 15
    spam 3

    Here’s the function I created to echo a categories comment count on a category page. Hope it helps someone too

    //function cat_comment_number() {
    //global $wpdb, $categories;
    //$categories = get_the_category();
    //$category_id = $categories[0]->cat_ID;
    //$ids = array();
    //$posts = get_posts( array(‘category’ => $category_id, ‘posts_per_page’ => 100) );
    //foreach( $posts as $post )
    // $ids[] = $post->ID;
    //$ids = implode( “,”, $ids );
    //$count = $wpdb->get_results( ”
    // SELECT comment_approved, COUNT(*) AS num_comments
    // FROM {$wpdb->comments} WHERE comment_post_ID in ($ids) AND comment_approved =1
    // GROUP BY comment_approved”, ARRAY_A );

    //echo $count[0][‘num_comments’];

    //}

  2. Hello Catzie, thanks for share this code.I have a troulbe with this snippet. I’m using the WP 3.4.I need count the depth of the pages’ or count only the parent page inside About’ like this example:About- Company Research Research Dossier-ContactIn this case the total is 3. It’s possible?Thanks / I’m willing to pay for the code

Create Comment

Your email address will not be published. Required fields are marked *