Wordpress 仅显示作者的最新评论
嗨,我有代码在我的模板中显示注释列表。我的问题可能只显示作者的最新评论。示例:如果作者B在同一篇文章中发表了3条评论,我只想显示作者B的最新评论,并排除/隐藏作者B的其他评论 这是我的代码:Wordpress 仅显示作者的最新评论,wordpress,comments,Wordpress,Comments,嗨,我有代码在我的模板中显示注释列表。我的问题可能只显示作者的最新评论。示例:如果作者B在同一篇文章中发表了3条评论,我只想显示作者B的最新评论,并排除/隐藏作者B的其他评论 这是我的代码: <table class="table table-striped"> <thead class="btn-primary bottom-margin"> <tr> <th&g
<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>
表格ID
主题
作者
日期
您必须使用“GROUP BY”子句从每位作者处获取最新评论,并且函数“get_comments()”不接受“GROUP BY”的参数。(列出了支持的参数)
您必须通过get_results()函数查询comments表并传递MySql查询,因此代码如下所示:
$table_prefix = $wpdb->prefix; // Get the WP table prefix in your database
$table_name = "comments"; // Table name we need to query
$table = $table_prefix . $table_name; // prefixing the table name
$comments = $wpdb->get_results("SELECT * FROM (SELECT * FROM " . $table . " where comment_approved=1 order by comment_date desc)c group by comment_author order by comment_date desc");
现在,在$comments中,您拥有每个作者的最新评论,您可以根据需要进行循环
请注意,我是按作者姓名对评论进行分组的,您可以在“评论作者电子邮件”中进行分组,也可以通过“评论作者IP”进行分组
我希望这会有所帮助。您可以按照自己的方式对SELECT语句进行更改