I run a wp_query that fetches 4 posts from db with rand() and displays them in a grid to visitors.

What I need is to display the exact same posts the query fetched the first time, a second time to the same visitor, based on their interaction with the site.

But if I run a new query, it will just pick random posts again. Everything works on the site, the query does what it is supposed to.

But how do I store the first custom wp_query in a cookie or session in order to display the same posts again to the same visitor?

Is it best to store the post ID's and fetch them later via the cookie or how is it done?

  • Are you showing these posts during the same request (just in a different place on the site) or in another request? Jan 12, 2019 at 19:51
  • The function which holds the query is called two times on the same page. 1 time on page Load and another time once the visitor interacts in some way with the page that they are on.
    – re-boot
    Jan 12, 2019 at 20:09

1 Answer 1


If the function is called on the same page (during the same request), you don't have to store it in COOKIE nor SESSION.

All you need is to create global variable and store the query in that variable. This way you won't send them to user and won't have to store them in visitors browser.

So you can write it like this:

// first call
global $my_special_query;

$my_special_query = new WP_Query(...);

// second call
// global $my_special_query;  // if the second call is in another file, there is a chance you'll have to uncomment this line
if ( $my_special_query->have_posts() ) { ... }

But there are few things you'll have to remember:

  1. The query will remember its state. So if you call the_post in the first call, then the query will be already in that position in the second call. You can use rewind_posts method to change that.
  2. If second call is in another PHP file, there is a chance you'll have to tell PHP that this $my_special_query variable is a global one.

On the other hand...

If both calls are made in different requests, then you can't use a variable any more. So you'll have to store these posts in visitors browser.

Remember that using sessions/cookies cause some law problems in some countries and you'll have to deal with them. You can use either COOKIEs or SESSION to do this.

  • Okay, so, sorry for my "stupidity" here. But I'm struggling to understand how this works on a visitor level and not on a request level? How will this work when there are 50 visitors on that page at the same time, each visitor has been shown 4 different posts on pageload and those 4 posts unique to each visitor has to be shown to the same unique visitor once again during their time spent on that page.
    – re-boot
    Jan 12, 2019 at 21:06
  • Ok, it has nothing to do with WP any more, but... It’s more like “how PHP apps works?” question. Every request is independent - so it doesn’t matter if it is another visitor or the same visitor but another request. So if you want to keep some data from one request for another one, then you have to send it to visitors browser and store it there. But during one request you can use variables and use them for storing anything. And, it’s obvious, but to be safe I mention that too - two visitors can’t share one request - so they won’t have access to the same variable value on server. Jan 12, 2019 at 21:12
  • What I am trying to say is that, when a visitor lands on this page and the function is called, the query will be stored in the variable (I understand that). But when another visitor lands on the same page before the second call is made from the first visitor, won't the contents of the variable change and always contain the latest query made on the page?
    – re-boot
    Jan 12, 2019 at 21:17
  • No, he won’t. Another visitor is another request. You can’t get access to variables of another user - imagine what would it cause - I would be able to publish questions as you on this site for example ;) Jan 12, 2019 at 21:19
  • Ofc, I guess i'm just way to tuned in to using cookies and sessions right now, because I thought that was the only way. There is only one request made by the visitor so your example will work, and it will be much more quick and easy to implement than what I thought I had to. Thanks:)
    – re-boot
    Jan 12, 2019 at 22:05

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.