How to Count Comments on a Post in WP Programmatically

3 from 5
from 5 user
(Rate this post)

The get_comments_number function will retrieve the number of comments for a post in WordPress. Do you want to check how many comments does a post has? Use the get_comments_number function with its easy syntax to get comments numbers for a post using ID.

The reason you want to count the comments of a post is obvious. After all, comments are the primary way we communicate with our website/blog visitors.

We’ll review the get_comments_number function and try its usage in a working example.

get_comments_number function syntax

WP developers can use get_comments_number function to get the number of comments on a post.

This function accepts one argument for post reference.


$post_id can be a specific post’s ID or a post object.

Counting comments in WordPress using get_comments_number function

The easiest way to retrieve comments number is to use get_comments_number function in WP.

I’m going to show you, how you can retrieve the number of comments on a post with ID 1.

Working example of get_comments_number function:

$post_id = 1; //set the post id

What do you think about "How to Count Comments on a Post in WP Programmatically"?

Your rating submit was successfull.

Your rating submit was not successfull.