Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
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()) {
	
	comment_form();
	
} 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.

Examples

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.

About the Author
Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
The Tao of WordPress: Master the art of WordPress.
Welcome
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 »
.htaccess made easy: Improve site performance and security.
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.