WordPress: Get Count For Approved Comments Outside The Loop

In Tips and Tricks, WordPress by May 3rd, 2013

620 Views

If you are a Web Developer and you work with WordPress, then there may have been a situation where you needed to get the count for  comments that were only in “Approved” state for a specific post. Note that this is different from the total comments count. So do you know how to get the comments count for Approved comments in WordPress? If not, then this article is for you. In this article, I am going to share a very easy way to get the count for all comments that are either Approved, Moderated, or in Spam and in Trash.

How to get comments count in WordPress for different comment states (Approved, Moderated, Spam, Trash ) outside WordPress loop

In order to get the comments count outside of the loop, we use the following function:

In the above function, note that when you use it, you won’t directly get the comment count directly as wp_count_comments returns us an object. So rather you would have to point to the properties inside of the object to get it’s corresponding value. So let’s say we want to get the comments count for post with ID 10. I am going to list all possibilities below:

 Get Approved Comments count:

 Get Count of Comments that are in Moderation:

 Get Count of Comments that are in Spam:

 Get Count of Comments that are in Trash:

 Get the total count of comments, regardless of their state:

That’s it!

Do you know of any other ways to get comments count in WordPress? Feel free to suggest by commenting below.