comments - Display custom posts, ordered by most commented, without duplicates

I would like to display custom posts in template, they have to be ordered by most commented and without duplicates on th

I would like to display custom posts in template, they have to be ordered by most commented and without duplicates on the same page. This is the code that I have for now.

$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(); ?>

I would like to display custom posts in template, they have to be ordered by most commented and without duplicates on the same page. This is the code that I have for now.

$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(); ?>
Share Improve this question asked Jan 14, 2020 at 14:16 unforgivenunforgiven 133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Currently, it looks like you are looping through comments and then querying the posts from there. Could you use the comment_count order by parameter directly in the posts query itself?

For example:

<?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;

Source: https://developer.wordpress/reference/classes/wp_query/#order-orderby-parameters

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744836577a4596299.html

相关推荐

  • comments - Display custom posts, ordered by most commented, without duplicates

    I would like to display custom posts in template, they have to be ordered by most commented and without duplicates on th

    2天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信