logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Only show # of approved comments

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.

This question has been answered.

badnews | 01/10/12 at 2:00pm Edit

Previous versions of this question: 01/10/12 at 3:23pm

(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    01/10/12
    2:12pm
    Sébastien | French WordpressDesigner says:

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

    There is an URL when i can see this bug ?

    Previous versions of this answer: 01/10/12 at 2:12pm

  • avatar
    Last edited:
    01/10/12
    2:45pm
    Francisco Javier Carazo Gil says:

    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;
    }

    • 01/10/12 3:00pm

      badnews says:

      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!

  • avatar
    Last edited:
    01/10/12
    3:20pm
    Jurre Hanema says:

    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');

    • 01/10/12 3:23pm

      badnews says:

      Awesome!

      That works nicely. Thank you

This question has expired.



badnews, Gabriel Reguly, Christianto, Julio Potier voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.