I was interested to read the answer to this question on how to paginate posts correctly that are random ordered. Is it possible to paginate posts correctly that are random ordered?

My goal is to randomly order posts that appear in a certain category on my site, in this case artists. Here is the code for the loop. In my case I use the wp_pagenavi plugin

       <div class="news-posts">
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts('cat=6&orderby=rand&posts_per_page='.get_option('posts_per_page').'&paged=. $paged);?>
    <?php wp_pagenavi(); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
    <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
     <?php //include (TEMPLATEPATH . '/inc/meta.php' ); ?>
     <div class="entry">
        global $more; $more = 0;
        the_content("Continue Reading",FALSE); 
       <div class="clear"></div>
    <?php endwhile; ?>
    <?php //include (TEMPLATEPATH . '/inc/nav.php' ); ?>
    <?php wp_pagenavi(); ?>
    <?php else : ?>
    <h2>Not Found</h2>
    <?php endif; ?>
    <?php wp_reset_query(); ?>

I used the filter to modify the ORDER BY statement of WP_query from the link above


add_filter('posts_orderby', 'edit_posts_orderby');

function edit_posts_orderby($orderby_statement) {

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

$orderby_statement = 'RAND('.$seed.')';
return $orderby_statement;

and this works fine when the artists page is viewed. As explained a random number is generated on the first page load then this is stored in a SESSION variable and used as the $seed for further paginated requests. This ensures that any potential for repeated artists posts is eliminated and a fresh set of artists posts is returned when the user clicks on page 2 or 3 of the pagination

However posts in different categories including events and news also get treated randomly which isn't what is desired.

I'd like these to be the latest first. I tried adding orderly = DESC but this makes no difference to that loop. The function from the previous post appears to override it. Is there a method of targeting just the artist category to be random within the seed function and to leave other loops alone.


1 Answer 1


You can use the add_filter line inside the template itself & no filter in the "functions.php" file. Then just after retrieving the posts remove that filter with a call to remove_filter. The actual function may still reside in the functions.php file

add_filter('posts_orderby', 'edit_posts_orderby');
// get all the posts for this specific loop here
remove_filter('posts_orderby', 'edit_posts_orderby');

This ensures that no other loop gets affected from this filter

In case you want to use another way, you can change the filter to accept 2 parameters, the 2nd parameter here is the concerned WP_Query object. In that object, you can examine the query parameters to determine weather to apply the filter or not

add_filter('posts_orderby', 'edit_posts_orderby', 10, 2);
function edit_posts_orderby($orderby_statement, $query) {
    if($query->get('cat') != 6)
        return $orderby_statement;
    // apply the random seed here
  • 1
    Or use remove_filter( current_filter(), __FUNCTION__ ); as the first line in the callback.
    – fuxia
    Nov 10, 2012 at 20:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.