显示自定义帖子,按最多评论排序,无重复

时间:2020-01-14 作者:unforgiven

我想在模板中显示自定义帖子,他们必须按大多数评论排序,并且在同一页上没有重复。这是我现在拥有的代码。

$args = array(
\'post_type\' => \'portfolio\',
\'status\' => \'approve\',
\'post_status\' => \'publish\',
\'number\' => 28,
\'order\' => \'ASC\',
\'orderby\' => \'comment_count\',
);
$comments = get_comments($args);
if ($comments) {
    foreach ($comments as $comment) : ;
        $post_args = array(
            \'post_type\' => \'portfolio\',
            \'posts_per_page\' => 1,
        );
        $posts = get_posts($post_args);
        foreach ($posts as $post) : setup_postdata($post);
            $title = get_the_title($comment->comment_post_ID);
            ?>
            <div class="col-md-3 col-sm-6 col-xs-12">
                <h4><?php echo $title; ?></h4>
            </div>
        <?php
        endforeach;
    endforeach;
}

?>
<?php wp_reset_query(); ?>

1 个回复
最合适的回答,由SO网友:Tom 整理而成

目前,您似乎正在循环浏览评论,然后从那里查询帖子。你能用comment_count 是否直接在posts查询本身中按参数排序?

例如:

<?php
$post_args = array(
    \'post_type\' => \'portfolio\',
    \'posts_per_page\' => 1,
    \'orderby\' => \'comment_count\'
);
$posts = get_posts($post_args);
foreach ($posts as $post) : setup_postdata($post);
    $title = get_the_title($comment->comment_post_ID);
    ?>
    <div class="col-md-3 col-sm-6 col-xs-12">
        <h4><?php echo $title; ?></h4>
    </div>
<?php
endforeach;
资料来源:https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

相关推荐

I cannot view the comments

我最近开发了我的wordpress网站www.fingeroffury。com用于写博客。我已经贴了几篇博文,获得了评论。遗憾的是,这些评论并没有出现在个别帖子上。我已经在设置讨论和个人帖子中检查了所有必要的权限,但仍然没有显示这些权限。我还能错过什么?