Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在WordPress中按最后评论日期排序帖子_Php_Wordpress - Fatal编程技术网

Php 在WordPress中按最后评论日期排序帖子

Php 在WordPress中按最后评论日期排序帖子,php,wordpress,Php,Wordpress,我使用了get_comments()和get_post()的组合来显示按评论日期排序的帖子列表 参考: 我的代码: $args = array( 'status' => 'approve', 'order' => 'DESC' ); $comments = get_comments( $args ); foreach ( $comments as $comment ) { $post = get_post( $comment->comment

我使用了
get_comments()
get_post()
的组合来显示按评论日期排序的帖子列表

参考:

我的代码:

$args = array(
    'status' => 'approve',
    'order' => 'DESC'
);

$comments = get_comments( $args );

foreach ( $comments as $comment ) {

     $post = get_post( $comment->comment_post_ID );
     echo $post->post_title;

}
这种方法的问题是,如果一篇帖子有多条评论,那么帖子列表将包含重复的帖子标题

如何删除重复项

更新: 这种最初的方法做了很多不必要的腿部工作。例如,在有大量帖子和评论的网站上,查询将收回大量不必要的评论

更新问题:
如何按评论日期排列帖子列表?

这是一种有趣的方法……但是,我想您可以用帖子ID填充一个数组,然后稍后再检查它们

$args = ....
$ids = array();
.....
if(!in_array($post->ID, $ids):
    $ids[] = $post->ID;
    echo $post->title;
endif;

感谢@Ohgodwhy,
in_array()
似乎是解决方案!出于兴趣,你会推荐一种完全不同的方法吗?还是你认为我应该坚持我最初的方法?你让我思考我最初的方法。我实际上认为它做了很多不必要的跑腿工作。在有大量帖子和评论的网站上,查询将收回大量不必要的评论。我现在在想,我怎么能按评论日期排列一个帖子列表呢?