3

I have a custom post type (gs_index) with commments enabled, and would like to display a list of recent comments only from that particular post type (outside of the loop)

I would also like to display the standard blog posts' comments without the comments from the custom post type.

Here is the code I am currently using

<ul>
  <?php
    $comments = get_comments('number=10&status=approve');
    $true_comment_count = 0;
    foreach($comments as $comment) :
  ?>

  <?php $comment_type = get_comment_type(); ?>
  <?php if($comment_type == 'comment') { ?>  
  <?php $true_comment_count = $true_comment_count +1; ?>  
  <?php $comm_title = get_the_title($comment->comment_post_ID);?>
  <?php $comm_link = get_comment_link($comment->comment_ID);?>
  <?php $comm_comm_temp = get_comment($comment->comment_ID,ARRAY_A);?>
  <?php $comm_content = $comm_comm_temp['comment_content'];?>

    <li>
      <span class="footer_comm_author">
        <?php echo($comment->comment_author)?>
      </span> on 
      <a href="<?php echo($comm_link)?>" title="<?php echo $comm_title?>">
        <?php echo $comm_title?> 
      </a>
    </li> 

  <?php } ?>
  <?php if($true_comment_count == 5) {break;} ?>
  <?php endforeach;?>
</ul>
  • Can you add the code where you register your custom post type? This would be helpful. – hakre Nov 23 '10 at 23:49
1

It's not well documented, but according to the codex, you can pass a 'type' variable in the get_comments function. Give this a shot

$comments = get_comments('number=10&status=approve&type=YOUR_POST_TYPE');
| improve this answer | |
  • 1
    type is for the type of comment, like normal (empty), pingback or trackback. So it will not select based on the post type the comment is attached to. – Jan Fabry Nov 25 '10 at 20:28
  • my mistake. dang ambiguous phrasing :) – Norcross Nov 27 '10 at 0:39
1

What you try to achieve is a missing feature with the get_comments() function in WordPress. So it is basically not possible to get comments by post-type with the get_comments() function so far.

The related ticket is: Ticket #12904 - get_comments(): Enable post_status, post_type

You will need another function that is returning the data you are looking for. Probably you need to write one on your own.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.