How to sort posts with the first 2 or 3 by latest, and the rest is random?

admin2025-06-03  2

I'm trying to sort my posts in the way I stated in the title.

What's the simplest way to do this?

I know how to change the order by object using the pre_get_post hook. But how can I use it to get what I need?

EDIT: here's my code so far

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

This is the idea, but i can't seem to make it work like i want it to. got the idea from here

I'm trying to sort my posts in the way I stated in the title.

What's the simplest way to do this?

I know how to change the order by object using the pre_get_post hook. But how can I use it to get what I need?

EDIT: here's my code so far

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

This is the idea, but i can't seem to make it work like i want it to. got the idea from here

Share Improve this question edited Feb 18, 2019 at 16:45 kberStill asked Feb 18, 2019 at 15:55 kberStillkberStill 233 bronze badges 3
  • 1 Two loops: "Show 3 most recent posts" (keep IDs in mind) and "Show all except 3 most recent IDs, 'orderby'=>'rand'"? – Max Yudin Commented Feb 18, 2019 at 16:06
  • Hello @MaxYudin thanks for the response. Can I do this and just attach it to the pre_get_post hook? Also do you have any example I can see to fully understand what you mean? – kberStill Commented Feb 18, 2019 at 16:22
  • keep in mind, that with order random you might get in trouble when paging.. plus i don't like the idea of using 2 queries.. but right now i'm not coming up with a better solution, eather. sorry.. – honk31 Commented Feb 18, 2019 at 17:47
Add a comment  | 

1 Answer 1

Reset to default 0

EDIT: Here is the correct answer...

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

You need to set the variables $number_of_recent_posts & $number_of_random_posts.

P.S - where do you work? - I am coming for your job c:

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748889512a314567.html

最新回复(0)