php - How to store post ID's in cookie or session to display the same posts later

admin2025-06-04  1

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?

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?

Share Improve this question edited Jan 8, 2019 at 17:17 re-boot asked Jan 8, 2019 at 17:00 re-bootre-boot 479 bronze badges 2
  • Are you showing these posts during the same request (just in a different place on the site) or in another request? – Krzysiek Dróżdż Commented 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 Commented Jan 12, 2019 at 20:09
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749017911a315658.html

最新回复(0)