WordPress.org

Ready to get started?Download WordPress

Forums

Exclude category from recent comments list (7 posts)

  1. TrishaM
    Member
    Posted 4 years ago #

    I have a snippet of code to display recent comments that works great, but I've created a category that is 'private' and need to exclude any comments made on posts from that category from showing up in the list of recent comments.

    Here is what I'm using to display the list of recent comments:

    <?php
    global $comment; if ( $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved='1' AND comment_type='' ORDER BY comment_date_gmt DESC LIMIT 10") ) : ?>
    <ul>
    <?php foreach ($comments as $comment) {
      echo '<li>' . sprintf('%s <span style="text-transform: lowercase;">on </span>%s', get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '">' . get_the_title($comment->comment_post_ID) . '</a>');
      echo '</li>'; } ?>
    </ul>
    <?php endif; ?>

    I'm not sure how, or even if, it can be modified to exclude a single category's comments.

    Any ideas?

  2. MichaelH
    Member
    Posted 4 years ago #

    Didn't test this but it should give you all the categories for a post:

    $cats = wp_get_post_categories($comment_>comment_post_ID);
    if ($cats) {
    $first_cat = $cats[0];
    }

  3. TrishaM
    Member
    Posted 4 years ago #

    Hi Michael

    Thanks for posting this - I've been playing around with it and it does give me the categories, but I'm still struggling to get it incorporated into my recent comments code so that it will display the recent comments EXCEPT when the post has a specific category.

    I though it should probably be in the SQL query part (above) and be something like where $cats!='112' AND comment_approved='1' but that doesn't work....

    So I'm guessing then it should go in the if statement part (above) but I'm not sure how to concatenate the $comments - $wpdb->get results with the $cats!='112'

    Any suggestions?

  4. TrishaM
    Member
    Posted 4 years ago #

    Bump.....still hoping to get some help with this, if it's possible for it even to be done.....

    Thanks!

  5. TrishaM
    Member
    Posted 4 years ago #

    Bump.....

  6. TrishaM
    Member
    Posted 4 years ago #

    Bump....

    It doesn't seem like this would be hard for a PHP programmer to figure out how to structure.....

    Basically just test a comment to see what category the post it's from is in, and if in a specifically category, exclude it from the list of recent comments...

    Is this really something that can't be done?

  7. mtw28
    Member
    Posted 4 years ago #

    Here's a simple way to skip outputting a comment in category 112 with the method you suggested:

    <?php
    if (in_category( '112', $comment->comment_post_ID )){
    continue;
    }
     ?>

    Put that in the foreach loops, like this:

    <?php
    global $comment; if ( $comments = $wpdb->get_results("SELECT comment_author, comment_author_url, comment_ID, comment_post_ID FROM $wpdb->comments WHERE comment_approved='1' AND comment_type='' ORDER BY comment_date_gmt DESC LIMIT 10") ) : ?>
    <ul>
    <?php foreach ($comments as $comment) {
    if (in_category( '112', $comment->comment_post_ID ){
    continue;
    }
      echo '<li>' . sprintf('%s <span style="text-transform: lowercase;">on </span>%s', get_comment_author_link(), '<a href="'. get_permalink($comment->comment_post_ID) . '#comment-' . $comment->comment_ID . '">' . get_the_title($comment->comment_post_ID) . '</a>');
      echo '</li>'; } ?>
    </ul>
    <?php endif; ?>

    A nicer way might be to change your sql to never select comments in that category, using this pseudocode "and comment_id not in (select comment_ids where post_taxonomy_id = 112)", but the above php code should work too.

Topic Closed

This topic has been closed to new replies.

About this Topic