Save up to 25% on pro security plugins w/ our Security Bundle »
Web Dev + WordPress + Security

WordPress get_comments_number() Not Working Correctly?

It seems that the WordPress function, get_comments_number(), is not working correctly. It is returning inconsistent results for posts with zero comments, pingbacks, or trackbacks. Sometimes the function returns true, other times false. It’s just random, from what I’ve been able to tell. So maybe a bug, or maybe something I am missing. Not a big deal, just looking for clues..

What’s happening

Some context. A lot of posts here at Perishable Press have comments closed. Some of those posts have existing comments, others do not. For posts that have comments closed and no existing comments, get_comments_number() sometimes returns true, and other times returns false. From what I can tell, it’s just random.

Here is the (simplified) template code in single.php:

if (comments_open()) {
} else { // comments closed
	if (get_comments_number()) {
		echo ('Comments are closed for this post.');

As explained, this code gives inconsistent results for posts with comments closed and no comments. On some of these posts, the message is displayed, “Comments are closed for this post.” Yet on other posts, the message is not displayed.


Some live examples. On the following posts, get_comments_number() returns true and the “Comments are closed” message is displayed:

But on these posts, get_comments_number() returns false and the “Comments are closed” message is not displayed:

Programmatically, there is no difference between any of the above posts:

  • They all have comments closed
  • The all have zero comments

Digging deeper..

Trying to figure this out, I entered the following SQL query (four times) to get the number of comments on each the above example posts:

SELECT * from wp_comments WHERE comment_post_ID='429'

So getting the comment count directly from the WordPress database, all of the above example posts returned zero comments.

Pro Tip: Working with the WordPress database? Check out my book, Wizard’s SQL Recipes for WordPress. Features many SQL recipes, tricks & tips!

Next test, I set up a custom comment query to check for any existing comments:

$args = array('post_id' => get_the_ID(), 'type' => array('comment', 'pings'));
$comments_query = new WP_Comment_Query;

$comments = $comments_query->query($args);
if (!empty($comments)) {
	foreach ($comments as $comment) {
		echo '<p>'. $comment->comment_content .'</p>';
} else {
	echo '<p>No comments found for post '. get_the_ID() .'</p>';

This test confirmed there are no comments on any of the example posts.

Final check for any existing post comments by adding this to the loop:

$comments_count = wp_count_comments(get_the_ID());

echo '<p>Comments for post ID '. get_the_ID() .':</p>';

echo 'Comments in moderation: ' . $comments_count->moderated      .'<br>'; 
echo 'Comments approved: '      . $comments_count->approved       .'<br>';
echo 'Comments in Spam: '       . $comments_count->spam           .'<br>';
echo 'Comments in Trash: '      . $comments_count->trash          .'<br>';
echo 'Comments Total: '         . $comments_count->total_comments .'<br>';

Again, zero comments on any of the four example posts.

There it is

There must be some other/confounding factor at play here. Just have not been able to track it down so far. I spent probably too much time investigating and reporting this, admittedly minor issue. So maybe someone reading is familiar and has some ideas. Either way, it’s a real puzzler.

Jeff Starr
About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
GA Pro: Add Google Analytics to WordPress like a pro.
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »
Fall season almost here :)
My greatest skill on social media is the ability to simply ignore 98% and keep scrolling without interacting.
Enjoying this summer, getting some great positive energy. Refreshing and inspiring.
☀️ Pro plugin giveaway! Enter to win 1 of 4 lifetime licenses for our WordPress security plugins, including 10-site Security Bundle!
There is no end to what humans can achieve when they work together.
Excellent (and free) tool to test your site's SSL configuration.
Plugin updates! All of our free and pro plugins ready for WordPress 6.2.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.