如何对WP_QUERY选择的WordPress帖子进行排序

时间:2020-12-09 作者:Andrew

英语不是我的第一语言,对不起:)

我需要不同的分页分页排序。

例如,我创建了循环:

 $qargs = array(
                    \'posts_per_page\' => 15,
                    \'no_found_rows\'  => true,
                    \'order\' => \'DESC\',
                    \'post_type\' => \'post\',
                    \'suppress_filters\' => false,
                    \'orderby\' => \'post_views\', // here I order my posts by views
                    \'fields\' => \'\',
                    \'date_query\' => array(
                        array(
                            \'after\'     => \'-7 days\',
                            \'column\' => \'post_date\',
                        ),
                    ),
posts_query = new WP_Query( $qargs );
现在,我可以显示15篇按视图排序的选定帖子,如下所示:

if ( $posts_query->have_posts() ): ?>
                while ( $posts_query->have_posts() ):
                    $posts_query->the_post();
.......
但我需要选择15个浏览量最大的帖子after that 按日期排序。

有可能吗?

非常感谢。

1 个回复
SO网友:Andrew

我找到了重新排序已选定帖子的方法。

例如,我们选择了以下帖子:

$qargs = array(
                \'posts_per_page\' => 15,
                \'no_found_rows\'  => true,
                \'order\' => \'DESC\',
                \'post_type\' => \'post\',
                \'suppress_filters\' => false,
                \'orderby\' => \'post_views\', // here I order my posts by views
                \'fields\' => \'\',
                \'date_query\' => array(
                    array(
                        \'after\'     => \'-7 days\',
                        \'column\' => \'post_date\',
                    ),
                ),
$posts_query = new WP_Query( $qargs );
现在我们可以按日期重新排列这些帖子:

function order_by_date( $a, $b ) {
        return strcasecmp( $b->post_date, $a->post_date );
    }
usort( $posts_query->posts, \'order_by_date\' );
接下来,我们可以回应我们的帖子:

if ( $posts_query->have_posts() ): ?>
            while ( $posts_query->have_posts() ):
                $posts_query->the_post();
.......