In WordPress, there is a built-in function called wp_count_comments that allows you to get the total number of comments for a specific post based on its ID. The function returns the total number of pending comments, approved comments, spam comments, comments in the trash, and even the total comment count.
However, the function does not allow you to retrieve the comment count in specific cases, such as for a specific category or for a number of tags/categories. We need to build our own database query to get the desired results.
In this (translated) post, we will present several snippets that allow you to obtain the total comment count for a site or a specific post, as well as the comment count for some special scenarios.
Total Comment Count for a Specific Post ID
The following code will display the total comment count for a post with the ID 43:
$comments = wp_count_comments( 43 );
echo 'Comments for post 43<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 />';
Total Comment Count for the Current WordPress Site
This code will display the total comment count for the current site:
$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 />';
Total Comment Count for Multisite
The previous code allows you to get the comment count for the current blog (even on a Multisite site). However, if you want to get the total comment count for another blog (with an ID of 2) on a Multisite site, you should use the following code:
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 );
If you use print_r( $count );
, you will get results like this:
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
)
)
Displaying Comment Count for a Specific Author
You can use the following query to get the total comment count for a specific author on a WordPress site:
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 );
Comment Count for Comments Created on a Specific Date
global $wpdb;
$where = $wpdb->prepare( "
WHERE YEAR(comment_date) = '%d'
AND MONTH(comment_date) = '%d'
AND DAYOFMONTH(comment_date) = '%d'",
2018, 10, 14
);
$count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$wpdb->comments} $where GROUP BY comment_approved", ARRAY_A );
You can also use the following format:
$where = $wpdb->prepare( "WHERE DATE(comment_date) = '%s'", '2013-10-29' );
Comment Count Between Different Dates
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 );
Total Comment Count for the Current Time (Day, Month, or Year)
For the current day:
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"]
);
To get the results for these, use the following code:
$count = $wpdb->get_results( "SELECT comment_approved, COUNT(*) AS num_comments FROM {$wpdb->comments} $where GROUP BY comment_approved", ARRAY_A );
Total Comment Count for a Specific Parent Comment
You should change the comment ID in line number 2:
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 );
Comment Count for a Specific User ID
You should change the user ID in line number 2:
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 );
Total Comment Count for Term IDs
The following code will fetch posts in categories with IDs 9 and 11 and then get the total comment count for all posts in those categories. You can use this code for tags or any custom taxonomies you’ve created:
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
);
This post is translated from the original source…