I'm working on a Drupal 7 site that lists hotels at the moment. To make it fair on the hotels who have signed up the listing page needs to be in a random order on each visit. You can click through to view a hotel's details details on a separate page. I need the site to "remember" the order the hotels were in so that when you go back to the listings they're in the same order and so the next/previous buttons work on the hotel details page.

I'm currently listing the hotels in a view so I'm a bit stumped as to how I can get this to work.


4 Answers 4


You can setup Views to cache the query. This will probably be globally, (meaning the every one will see the same list for X time). It's not bulletproof but it's an easy solution. Alternatively you need to do some cache per user, which could be quite tricky or some custom code.


By default Views allow you to clear the cache every 5 min (and other predefined values), if you add the cache actions module, you get rules based clearing of cache. With the use of rules you are able to setup the clear cache event to fire at a specific time of day (triggered by cron) and other special conditions that you are able to build with the power of rules.

  • This works. Thanks. Do you know if I can set the cache to clear at a set time or is it just every X hours? (I'll accept once I know that).
    – SpaceBeers
    Commented Jan 23, 2012 at 16:52
  • @SpaceBeers Updated my answer, as it was a little more than was fitting for a comment.
    – googletorp
    Commented Jan 23, 2012 at 17:41

First of all, in order for the View to "remember" your random order, you will need to cache it. But that will show the same View to every user. The simple solution is to set a relatively short cache time, so that you get a new random order, say every ten minutes.

However, since you would like to take it a step further, you can do the caching "per user context". This will be quite complicated, but I will do my best to explain how you can do this.

I will suggest you use Panels and Page manager, to accomplish this. Both modules are fairly complex tools, that require some understanding to make work properly. I will provide an outline below, and then I suggest these videos, for further information.

I make the assumption here that you have a dedicated "listing page". The solution should be applicate in order situations as well, such as the list being shown on a node-view, but will be slightly different.

  1. Install the Chaos tool suite (ctools) module, and Panels, and enable "Page manager", "Views content panes", as well as "Panels".
  2. Go into your View, and add a display of type "Content pane", leave it un-cached.
  3. Go to admin/structure/pages, and enable create a "custom page".
  4. Create a variant of type "Panel", pick any layout for now.
  5. Add your your content pane to the panel page.
  6. Go to Contexts", in the left menu. Add a "User" context, selecting "Logged in user".
  7. Go back to "Content", and in the top right of your view pane, click the little cog, go down to "Caching", and select "Change".
  8. Pick "Simple cache", any timeframe you want, and a granularity of "Context".

Now, you will get one individual View cache, per logged in user. If you want to make this work for anonymous users, things will become further complicated, since it's not obvious "who is who", so to speak. We first need to agree on what constitutes "a user". A simple answer would be "an ip-adress". Then we need to explain this concept to Ctools, but implementing a new Ctools context. Doing so is well beyond the scope of this question, but there is an example in /ctools/ctools_plugin_example/plugins/contexts/simplecontext.inc.


An alternative to caching might be to use the user id (or other integer derived from the session ID) as the random seed. This way each user would always see the same order. Views is adding ORDER BY RAND(), and I think hook_views_query_alter could be used to change this to ORDER BY RAND($uid).


Not sure if this is exactly what you're after, but I use Views random seed. This allows you to select "Global: Random seed" as a sort criteria in Views and then set a unique 'seed' for each user and set the time in which the 'seed' resets.

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.