Is it possible to paginate posts correctly that are random ordered?

I found this problem on WordPress Support and the topic unfortunately is now closed. I have this same issue… (read below)

We have created a site where members can recommend things like favourite books, movie, songs, etc. For this problem I will use the Movies page as an example.

The “Movies” page is ultimately a custom page template that asks wordpress to display a random list of ALL posts that have been given the category “movies” (category 31). It displays the title of these movies in random order using the code below.

$rand = new WP_Query("cat=31&showposts=-1&orderby=rand"); 
while($rand->have_posts()) : $rand->the_post();
?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
<?php endwhile; ?>

Problem is the list is getting rather long and I would like to split it up into two or more pages of about 10 movies each. To achieve this, I have used the code below.

$page = (get_query_var('paged')) ? get_query_var('paged') : 1; 
while ( have_posts() ) : the_post() 
?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?>
<?php endwhile; ?>

But there is a problem because although it splits up the data into pages of 10 posts each (paginates), it fails to include a new set of 10 posts on page 2 and so forth. In other words, because it is listing things in a random order, it just goes out and gets another 10 random posts (or in this case movie titles). As a result, we have some repeated movie title posts instead of a new set of 10 random movie titles on page 2, etc.

My question is – what can I add to this code to get wordpress to “remember” which random 10 posts it included on page 1, and then have it get a new set of 10 posts to put on pages 2, 3, etc. until all posts are displayed. I would like there to be only one occurrence of a post per page when sorted randomly in sets of 10.


You can use a filter to modify the ORDER BY statement of WP_query.

That way you can manually set the query to use ORDER BY RAND($seed);

Mysql RAND() accepts a seed as an optional argument. Using a seed, it will return the same randomized result set each time.

So you can generate a random number on the first page load then store it in a SESSION variable and use that as the $seed for further paginated requests.

If you are trying to acheive this on your main loop for instance, you could add the following to your functions.php file.


add_filter('posts_orderby', 'edit_posts_orderby');

function edit_posts_orderby($orderby_statement) {

    $seed = $_SESSION['seed'];
    if (empty($seed)) {
      $seed = rand();
      $_SESSION['seed'] = $seed;

    return $orderby_statement;

Leave a Comment