in my main loop i show only posts of the 'artworks' post type, orderby=rand.

when i view a single artwork and use the next / prev nav, it disregards the main query and goes by chronological order - as described in the codex: http://codex.wordpress.org/Template_Tags/next_post_link

how can i store and use the random order generated at first to alter the post navigation order? any ideas?

the order would be randomized upon the reload of the home page, but within each session each 'random order' should be consistent.

so, for example:

1- home page i see posts in a random order (4,7,1,8,6,3). i click on post 8.

2- on the single view of post 8, the 'next' post would be 6, and the previous would be 1. etc..

3- i should be able to go through back and forth and the order would be consistent.

4- if i click on the title of the website or reload it again (not just history>back, but an actual reload). then there should be a new random order.

  • Could you elaborate on your requirements a bit? Storing random order would kind of make it not-random-anymore.
    – Rarst
    Commented Jul 2, 2013 at 21:20
  • i just edited the post to make it more detailed Commented Jul 2, 2013 at 21:36
  • Pretty sure this isn't possible - at least not without some serious re-engineering.
    – vancoder
    Commented Jul 2, 2013 at 23:27
  • well, i would imagine that you can somehow save the order into the mysql db and then retrieve it to find the adjacent posts, any clues how to do that? Commented Jul 3, 2013 at 0:05

1 Answer 1


after a lot of trial and error WP transients came to save me!!

here is the solution for anyone that might need this...

in the index.php (outside the main loop) i save the current random order in a transient:

$order_array = array();
while ( $the_query->have_posts() ) :
        $order_array[] = get_the_ID() ;

set_transient('post_order', $order_array, 100);
// the expiration can be whatever you want

// Restore original Post Data

now in the single.php where i add the next and prev navigation functionality:

            $next_post_index = $current_post_index+1;
            echo "<br> next post index : " . $next_post_index;
            $next_post_id = $array[$next_post_index];
            echo " id: " . $next_post_id;
            if($next_post_id != '') {echo "<br><a href=" . get_permalink( $next_post_id ) . ">next</a>";}

            $previous_post_index = $current_post_index-1;
            echo "<br> previous post index : " . $previous_post_index;
            $previous_post_id = $array[$previous_post_index];
            echo " id: " . $previous_post_id;
            if($previous_post_id != '') {echo "<br><a href=" . get_permalink( $previous_post_id ) . ">previous</a>";}

this has a bit of extra markup because i was echoing everything along the way to check if it was working. i am sure that it can be cleaned up, but it works! and it does not display next or prev if there is no other post before or after it (i.e. first post only shows 'next' link)

yay, it works :)

  • Are you sure this is what you were looking for? I'm pretty sure you store this value globally for your WordPress and all of it's users and not individually for each user/session. So when multiple users will visit your page, they will mess each others random orders, I guess. But maybe I misunderstood something. Commented Aug 7, 2013 at 7:32
  • I'm not sure I'm following how you are grabbing the transient "post_order" in your second block of code. Commented Sep 23, 2015 at 1:30

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.