Hi I have code to display comment list in my template. my question is possible to display only newest comment from the author. Example: If author B has made 3 comment in same post, i want to display just newest comment from author B and exclude/hidden the other comments has made by author B.
Here my code :
<table class="table table-striped">
<thead class="btn-primary bottom-margin">
<tr>
<th>Form ID</th>
<th>Subject</th>
<th>Author</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php $args = array(
'status' => 'approve',
'number' => 0,
'order' => 'DESC'
);
$comments = get_comments($args);
foreach($comments as $comment) : $count++;?>
<?php $post_args = array(
'post_type' => 'ticket_system',
'p' => $comment->comment_post_ID,
'posts_per_page' => 50
);
$posts = get_posts($post_args);
foreach($posts as $post) : setup_postdata($post);?>
<tr>
<td class="col-md-2 flags"><?php echo get_post_meta($post->ID, "idticket",$single = true); ?></td>
<td class="col-md-6 flags"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title();?></a></td>
<td class="col-md-2 flags"><?php echo $comment->comment_author;?></td>
<td class="col-md-2 flags"><?php echo $comment->comment_date;?></td>
</tr>
<?php endforeach;
endforeach; ?>
</tbody>
</table>
You have to use “GROUP BY” clause to get the newest comment from each author and the function “get_comments()” does not take a parameter for “group by”. (Supported parameters are listed here).
You will have to query the comments table via get_results() function and pass the MySql query so code will look like this:
Now in $comments you have the newest comment from each author and you can loop through as you want.
Please note I am grouping the comments by author name you can do it for the “comment_author_email” or being very specific by “comment_author_IP”.
I hope this helps.