Title: Using cookies in query
Last modified: August 20, 2016

---

# Using cookies in query

 *  [formica](https://wordpress.org/support/users/formica/)
 * (@formica)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/using-cookies-in-query/)
 * Goal is to display a list of recent posts visited. (including thumbnails) I’ve
   set a cookie saving the post id’s of recent posts and if i echo $_COOKIE[‘pages_visited’];
   i get a successful result of ’60, 54, 33, etc’.
 * My question id how do I get this to work in a query, the code i have is:
 *     ```
       <?php $post_list = $_COOKIE['pages_visited']; ?>
   
       <?php query_posts('post_id=array('.$post_list.')&showposts=4&post_type=post_therapists'); ?>
   
       <?php while ( have_posts() ) : the_post(); ?>
   
       <p><?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } ?>
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
       <?php the_title(); ?>
       </a></p>
   
       <?php  endwhile; ?>
       ```
   
 * Any ideas anyone?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [formica](https://wordpress.org/support/users/formica/)
 * (@formica)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/using-cookies-in-query/#post-3050526)
 * Doh! Structured wrong. [Codex](http://codex.wordpress.org/Function_Reference/query_posts#Combining_Parameters)
 *     ```
       <?php $post_list = $_COOKIE['pages_visited']; ?>
       <?php  wp_reset_query();  // Restore global post data stomped by the_post(). ?>
       <?php query_posts( array('post__in' => array($post_list), 'orderby' => 'post__in', 'post_type' => 'post_therapists') ); ?>
        <?php while ( have_posts() ) : the_post(); ?>
       ```
   
 * _[Moderator Note: Please post code or markup snippets between backticks or use
   the code button. As it stands, your code may now have been permanently damaged/
   corrupted by the forum’s parser.]_
 *  Thread Starter [formica](https://wordpress.org/support/users/formica/)
 * (@formica)
 * [13 years, 10 months ago](https://wordpress.org/support/topic/using-cookies-in-query/#post-3050562)
 * Sorry was too hasty. For some reason the above won’t work without exploding the
   string see [here](http://wordpress.org/support/topic/multiple-specific-posts-using-wp_query-and-post_in-array-explained)
 * Correct code for putting a dynamic string in a query:
 *     ```
       <?php $post_list = $_COOKIE['pages_visited']; ?>
   
       <?php query_posts( array('post__in' => explode(',', $post_list), 'post_type' => 'post_therapists') ); ?>
   
       <?php while ( have_posts() ) : the_post(); ?>
       ```
   
 * Thanks ixwa for posting.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Using cookies in query’ is closed to new replies.

## Tags

 * [array](https://wordpress.org/support/topic-tag/array/)
 * [cookies](https://wordpress.org/support/topic-tag/cookies/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 1 participant
 * Last reply from: [formica](https://wordpress.org/support/users/formica/)
 * Last activity: [13 years, 10 months ago](https://wordpress.org/support/topic/using-cookies-in-query/#post-3050562)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
