This content originally appeared on Perishable Press and was authored by Jeff Starr
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:
- How to Verify the Four Major Search Engines
- Bloggers Toolbox: Strategic Elements for a Perfect About Page
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.
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.
This content originally appeared on Perishable Press and was authored by Jeff Starr
Jeff Starr | Sciencx (2023-01-21T21:43:33+00:00) WordPress get_comments_number() Not Working Correctly?. Retrieved from https://www.scien.cx/2023/01/21/wordpress-get_comments_number-not-working-correctly/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.