WP_Query: Show 10 posts in date order, first three random

I have looked online and found different pieces of code together but unable to find a solution that holds up.

Basically i need to display 10 posts in total, but the first three need to be random.

This is what I have so far (taken from a similar question).

$args = array(
‘post_type’ => ‘post’,
‘posts_per_page’ => -1,
‘orderby’ => ‘publish_date’,
‘order’ => ‘DESC’,
‘_shuffle_and_pick’ => 3 // <– our custom argument
);
$loop = new WP_Query( $args );

With the following function in my functions.php

add_filter( ‘the_posts’, function( $posts, WP_Query $query )
{
if( $pick = $query->get( ‘_shuffle_and_pick’ ) )
{
shuffle( $posts );
$posts = array_slice( $posts, 0, (int) $pick );
}
return $posts;
}, 10, 2 );

But this just displays three random posts and that’s all.

Can this be adapted so that the it displays 10 in total, with the first 3 being random the rest in date order?

Or do I need a new approach?

Read more here:: WP_Query: Show 10 posts in date order, first three random

Leave a Reply

Your email address will not be published. Required fields are marked *