WordPress.org

Ready to get started?Download WordPress

Forums

Using cookies in query (3 posts)

  1. formica
    Member
    Posted 1 year ago #

    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?

  2. formica
    Member
    Posted 1 year ago #

    Doh! Structured wrong. Codex

    <?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.]

  3. formica
    Member
    Posted 1 year ago #

    Sorry was too hasty. For some reason the above won't work without exploding the string see here

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic