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>
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
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 theget_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.