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.
get_comments_number($post_id)
$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
get_comments_number($post_id);
written by Mehdi Nazari about in WordPress WordPress Functions WordPress Plugin Development WordPress Theme Development
What do you think about "How to Count Comments on a Post in WP Programmatically"?