Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Only show # of approved comments WordPress

  • SOLVED

I am presently using <?php comments_number('No Comments', '1 Comment', '% Comments' );?> to show the number of comments per post.

The problem however is that if there are no approved comments for a post. The code above shows the total # of comments for that post (even if none is approved - so if there are 2 comments waiting moderation it shows "2 Comments").

I'd like it modified so in an instance where a post has no approved comments and a few comments pending moderation. Instead of showing the number of unapproved comments a generic blurb stating something along the lines of "No comments for this post yet" is displayed.

Answers (3)

2012-01-10

Francisco Javier Carazo Gil answers:

Hi Badnews,

In wp_comments table you should see comments. Well rows represent the next:
* comment_post_ID: post_ID foreign key
* comment_approved: 0 or 1

If you have this bug you could always do your own function and call it:

functions my_comments_number($zero, $one, $more, $post_id)
{
global $wpdb;

$comments_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = '1' AND comment_post_ID = '$post_id';" ) );

if($comments_count == 0)
echo $zero;
else if($comments_count == 1)
echo $one;
else
echo $more;
}


badnews comments:

Thank you, though I didn't use your suggested solution it lead me in the right direction. This is what had created the bug:

update wp_comments set comment_approved = '0' where comment_approved = '1'

I had used this query in mysql to mass un-approve comments. Would you suggest a better way?

Thanks!

2012-01-10

Jurre Hanema answers:

After you have un-approved all comments using your query, you can update the comment count of the posts with a query like this:


UPDATE wp_posts p SET comment_count = (SELECT COUNT(*) FROM wp_comments WHERE comment_post_ID = p.ID AND comment_approved = '1');


badnews comments:

Awesome!

That works nicely. Thank you

2012-01-10

Sébastien | French WordpressDesigner answers:

euh, if there is no approved comments, this code displays "No Comments".

There is an URL when i can see this bug ?