How To Display Posts in a Random Order and Retain Pagination Order

Snippets by IsItWP

Do you want to display posts in a random order while retaining the pagination order? This snippet will let you display a random list of random posts that stay in the same order for as long as the session exists. This makes sure that the pagination will be consistent.


All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:


add_filter( 'posts_orderby', 'randomise_with_pagination' );
function randomise_with_pagination( $orderby ) {

	if( is_front_page() ) {

	  	// Reset seed on load of initial archive page
		if( ! get_query_var( 'paged' ) || get_query_var( 'paged' ) == 0 || get_query_var( 'paged' ) == 1 ) {
			if( isset( $_SESSION['seed'] ) ) {
				unset( $_SESSION['seed'] );
		// Get seed from session variable if it exists
		$seed = false;
		if( isset( $_SESSION['seed'] ) ) {
			$seed = $_SESSION['seed'];
	    	// Set new seed if none exists
	    	if ( ! $seed ) {
	      		$seed = rand();
	      		$_SESSION['seed'] = $seed;
	    	// Update ORDER BY clause to use seed
	    	$orderby = 'RAND(' . $seed . ')';

	return $orderby;

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out our other articles on the site like: 7 Best Drag and Drop WordPress Page Builders Compared.

Comments   Leave a Reply

  1. Thank you!

    It works like a charm…. but it creates 9 additional post-pages at the end, landing to “404 Page Not Found”

    Any thoughts?

    Thank you in advance.


  2. Thank You! it works like a charm… but How about when using a Cache plugin?… I’ve purged all cache and still it doesn’t work in first page only.

    Any thoughts is appreciated.

    Thanks once again.


  3. exactly what I was looking for, thank you!!

  4. Hi, this code works very well. The only problem is that in the front page it also shows the “recent post” widget in randon order. Some idea how to change that?

    1. I didn’t notice it until you mentioned it but we have exactly the same problem, glad you pointed it out.

      Did you manage to solve it somehow?

  5. I put in the snippet using the snippet plugin. I am just setting up this site locally and only have 4 test posts. I do not notice the posts randomizing….at what point does the randomize happen?

  6. Riccardo Vezzani June 18, 2019 at 12:17 pm



    1. Glad the snippet was helpful

  8. Is this supposed to fix the ‘second page random posts’ issue? Or is it to illustrate the issue? I’ve used it to generate some randomly ordered posts, but when I go to the second page, the randomisation obviously starts again, because I may or may not get the same posts appearing.

  9. Thanks a lot. I had been looking for code like this to share more articles with the rrader at the end of my site.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!