Comment count doesn’t seem to be correct

7.94K viewsIssuesbug
0

Hi Rahul,

I followed the WordPress Codex for number of comments per question as below

https://codex.wordpress.org/Function_Reference/comments_number

However, this doesn’t seem to report the correct number. For example, I have over 15 comments on a particular question, but the codex function returns only two ?

Doesn’t seem to matter if this is inside the loop or out.

Any thoughts ?

2

Hello Mark,

If i am not wrong then you are trying to sum comment counts of question and answers. That is not a default behavior but this can be achieved.

Let me think adding this feature will be helpful or not.

Thanks

commented on answer

Personally, I think it’s a good idea. The higher the comments and views etc, the higher the engagement. It’s something I think should be core

Yeh, I agree.

1

Validated as working

$args = array(
‘count’ => true,
‘parent’ => get_the_ID()
);
$totalcomments = get_comments($args);

<!– Comment Count –>
<a class=”ap-questions-count ap-questions-ccount” href=”<?php echo ap_answers_link(); ?>”>
<span itemprop=”commentCount”><?php echo $totalcomments ?></span>
<?php _e( ‘Comments’, ‘anspress-question-answer’ ); ?>
</a>

Works as intended once the comment_parent mapping is correctly configured

commented on answer

Hello Mark,
I don’t think this snnipet is doing what you are trying to achive. parent argument is used to get child comments of a comment.

1

Looking at this further, it would be simple to achieve if we populate table wp_comments (using field comment_parent) and base the count on that

For example:

SELECT * FROM `wp_comments` WHERE comment_post_id = 7239

Returns 4 rows, but this is only the comments per ANSWER – not the entire thread. So, in an ideal world, we could use the ID of the question itself to populate the same comment_parent field. With this is mind, it would then be easy to calculate the count of comments based on each question, no ?

SELECT count(*) AS ‘Total’ from wp_comments WHERE ‘comment_parent’ = 0

(which would return 237 – not what we want. BUT – if we populate this value at the time of posting

UPDATE `wp_comments` SET comment_parent = 7232 WHERE comment_post_id = 7239;

Then re-run the same query

SELECT count(*) AS ‘Total’ from wp_comments WHERE comment_parent = 7232

This provides the desired (well, almost, as I’d need to set all comments in this thread to have the same parent) result

Can we implement something along these lines ??

edited answer
1

Here’s some examples. These are the actual questions, which have comments

And these are the answers, which have comments

What I’m looking for is a function that will combine the comments from both the actual question, plus the answers to the question, then provide the overall total 🙂

1

Ok, I see the disconnect. This is definitely a bug.

The WordPress codex will only show comments for the actual post itself – not the answers. In this case, there are two comments on the original question, so that would be correct. However, what I want is the cumulative total for all comments on all answers, plus the comments to the actual question