Support » Themes and Templates » Listing recent comments excluding a post ID

  • In my sidebar I’m using from the codex

    $comments = get_comments('post_id=-15');
      foreach($comments as $comm) :

    But I want to EXCLUDE all comments from post ID 15 You will see I’ve added a ‘-‘ symbol, but it does not work to exclude. How do I do this please?


Viewing 4 replies - 1 through 4 (of 4 total)
  • Get comments doesn’t work that way.

    Maybe this similar post will help:

    Thanks ipstenu, but the line `$comments = get_comments(‘post_id=15’);
    ` is correct to filter comments for only one id. I already use this and it works. My ID is in fact a page, not a post so I cant use a category as the other post suggests.

    There must be a way to prevent comments being listed for a specific page ID without using a plugin?

    Sorry, I was unclear. What I’m saying is that the NEGATIVE sign doesn’t work that way 🙂 There’s an include, but no exclude. I’d open trac and put this in as a request to add to WP.

    This code (which is from the link I gave you) skips category 112, so I suspect you could re-jigger it to have it check for in-post instead:

    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") ) : ?>
    <?php foreach ($comments as $comment) {
    if (in_category( '112', $comment->comment_post_ID ){
      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>'; } ?>
    <?php endif; ?>

    thanks, I see what you mean now, Ill see if I can work it out. Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Listing recent comments excluding a post ID’ is closed to new replies.