I use traded comments so each comments receive multiple replies in it. I need to get replies count of each individual comment. I don't need the total replies count of all the comments.
I tried to use this code:
$comment_id = get_comment_ID();
$childrenCount = get_comment_meta( $comment_id, "childrenCount" );
$childrenCountNumber = count($childrenCount);
echo $childrenCountNumber
The other way round that shows total replies which not what I'm looking for.. This code shows all the replies of all the comments.`
Code in functions.php
function replies_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;
}
Then echo out in the template page:
$parents_count = replies_counter($post->ID);
$children_count = $post->comment_count - $parents_count;
echo $children_count;
This one is all about getting total replies count of all the comments. But I need replies count of each comment individually.
It will be nice if someone help me.
why don't you write such a custom function to get subcomments for each individual comment by ID like this?
function subreplies_counter($id){
global $wpdb;
$query = "SELECT COUNT(comment_ID) AS count FROM $wpdb->comments
WHERE `comment_approved` = 1 AND `comment_parent` = $id";
$data = $wpdb->get_row($query);
return $data->count;
}
I always prefer to use a WordPress core function, if there is one to do what I want.
You can easily achieve that using get_comments. With the count
arg set to true
. This way it will retrieve only the comments count.
$args = array(
'post_id' => $post->ID, //main post id
'parent' => get_comment_ID(), //the comment id
'count' => true, //just count
);
$comments = get_comments($args); //number of comments;