前2或3的帖子如何按最新顺序排序,其余的是随机的?

时间:2019-02-18 作者:kberStill

我正试图按照标题中所述的方式对我的帖子进行排序。

最简单的方法是什么?

我知道如何使用pre_get_post 钩但是我怎样才能用它来得到我需要的东西呢?

编辑:这是我目前的代码

add_action(\'pre_get_posts\', \'filter_category_orderby\');

function filter_category_orderby($query){
if($query->is_category()){

    $query2 = $query;
    $query->set(\'orderby\',\'rand\');

    $wp_query = new WP_Query();
    $wp_query->posts = array_merge( $query2->posts, $query->posts );


    $wp_query->post_count = $query1->post_count + $query2->post_count;
    $query = $wp_query;
 }
}
这就是我的想法,但我似乎无法让它像我想要的那样工作。我是从here

1 个回复
最合适的回答,由SO网友:Jason Is My Name 整理而成

EDIT: 下面是正确的答案。。。

<!-- RECENT BLOG POSTS -->
<?php
$recentargs = array(
    \'post_type\' => \'post\',
    \'post_status\' => \'publish\',
    \'category_name\' => \'social-grid\',
    \'posts_per_page\' => $number_of_recent_posts,
    \'orderby\' => \'post_date\',
    \'order\' => \'DESC\'
);
$rec_arr_posts = new WP_Query( $recentargs );

if ( $rec_arr_posts->have_posts() ) :

    while ( $rec_arr_posts->have_posts() ) :
        $rec_arr_posts->the_post();
    ?>

        <?php get_the_post_thumbnail_url(); ?>
        <?php the_permalink(); ?>
        <?php the_title(); ?>
        <?php echo get_the_excerpt(); ?>

    <?php endwhile; ?>

<?php endif; ?>
<?php wp_reset_postdata(); ?>
<!-- END RECENT BLOG POSTS -->  

<!-- RECENT EXCLUDED POSTS -->
<?php
$recenttoexcludeargs = array(
    \'post_type\' => \'post\',
    \'post_status\' => \'publish\',
    \'category_name\' => \'social-grid\',
    \'posts_per_page\' => $number_of_recent_posts,
    \'orderby\' => \'post_date\',
    \'order\' => \'DESC\'
);
$recent_to_exclude_arr_posts = new WP_Query( $recenttoexcludeargs );

if ( $recent_to_exclude_arr_posts->have_posts() ) {
    $posts_ids = wp_list_pluck( $recent_to_exclude_arr_posts->posts, \'ID\' );
} ?>
<!-- END RECENT EXCLUDED POSTS -->
<!-- RANDOM BLOG POSTS -->
<?php
$randomargs = array(
    \'post_type\' => \'post\',
    \'post_status\' => \'publish\',
    \'category_name\' => \'social-grid\',
    \'posts_per_page\' => $number_of_random_posts,
    \'post__not_in\' => $posts_ids,
    \'orderby\' => \'rand\',
    \'order\' => \'DESC\'
);
$rand_arr_posts = new WP_Query( $randomargs );

if ( $rand_arr_posts->have_posts() ) :

    while ( $rand_arr_posts->have_posts() ) :
        $rand_arr_posts->the_post();
    ?>

        <?php get_the_post_thumbnail_url(); ?>
        <?php the_permalink(); ?>
        <?php the_title(); ?>
        <?php echo get_the_excerpt(); ?>

    <?php endwhile; ?>

<?php endif; ?>
<?php wp_reset_postdata(); ?>
您需要设置变量$number\\u of\\u recent\\u posts&$\\u random\\u帖子的数量。

P、 你在哪里工作我来找你的工作c:

相关推荐

Sort posts in a specific way

我目前有6个职位属于“服务”类别。每个服务都有\\u内容和1-2个文档(小册子和/或T&C以及外部表单)现在,假设这6篇文章的ID为1,2,3,4,5,6,但我希望它是2,5,3,1,4,6。这可能吗?如果是,我将如何实现它?