Save 25% on Wizard’s SQL for WP w/ code: WIZARDSQL
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.

Jeff Starr
About the Author
Jeff Starr = Creative thinker. Passionate about free and open Web.
WP Themes In Depth: Build and sell awesome WordPress themes.

Leave a reply

Name and email required. Email kept private. Basic markup allowed. Please wrap any small/single-line code snippets with <code> tags. Wrap any long/multi-line snippets with <pre><code> tags. For more info, check out the Comment Policy and Privacy Policy.

Subscribe to comments on this post

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 »
WP Themes In Depth: Build and sell awesome WordPress themes.
Thoughts
DIY: Monitor File Changes via Cron working perfectly for over a decade.
Mastodon social is a trip. Glad I found it.
As a strict rule, I never use cache plugins on any of my sites. They cause more problems than they solve, imho. Just not worth it.
Currently on a posting spree :)
6 must come before 7.
My top three favorite-to-write coding languages: CSS, PHP, JavaScript.
If you’re not 100% sure that you can trust something, you can’t.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.