Count parent comments and replies separately

This snippet will count parent comments and replies separately. Add the first snippet to the functions.php of your wordpress theme then add the second snippet in the single.php or the theme you will to display the number of comments and comment replies.

 

function c_parent_comment_counter($id){
global $wpdb;
$query = "SELECT COUNT(comment_post_id) AS count FROM $wpdb->comments WHERE `comment_approved` = 1 AND `comment_post_ID` = $id AND `comment_parent` = 0";
$parents = $wpdb->get_row($query);
return $parents->count;
}

<?php
$number_of_parents = c_parent_comment_counter($post->ID);
$number_of_children = $post->comment_count - $number_of_parents;
echo "parents: ".$number_of_parents;
echo "children: ".$number_of_children;
?>

Leave a comment