Skip to content

Instantly share code, notes, and snippets.

@wpjess
Created June 25, 2020 21:47
Show Gist options
  • Save wpjess/94136c3f0083a284b53c9922571265e6 to your computer and use it in GitHub Desktop.
Save wpjess/94136c3f0083a284b53c9922571265e6 to your computer and use it in GitHub Desktop.
Create a filter to randomize posts in a query
// RANDOMIZE A SPECIFIC NUMBER OF POSTS
add_filter( 'the_posts', function( $posts, \WP_Query $query )
{
if( $random = $query->get( 'jess_random_posts' ) )
{
shuffle( $posts );
$posts = array_slice( $posts, 0, (int) $random );
}
return $posts;
}, 10, 2 );
// add this var to your query to use: jess_random_posts' => 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment