Created
June 25, 2020 21:47
-
-
Save wpjess/94136c3f0083a284b53c9922571265e6 to your computer and use it in GitHub Desktop.
Create a filter to randomize posts in a query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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