Spring Sale! Save 30% on all books w/ code: PLANET24
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.

About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
Banhammer: Protect your WordPress site against threats.
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 »
USP Pro: Unlimited front-end forms for user-submitted posts and more.
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.