WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I exclude visited posts from a loop? I have a loop of recent posts on a single post page that excludes the current post and I want to extend the amount of times users browse by filtering out the ones they've already seen. Here's the basic loop.

 $featured_post_id = get_the_ID();
 global $post;
 $args = array('cat' => '9', 'posts_per_page' => 5, 'post__not_in' => array($featured_post_id));
 $my_query = new WP_Query($args);
 if ( $my_query->have_posts() ) : ?>
share|improve this question
up vote 0 down vote accepted

This is a heavier question than you think. You'll need to do quite a bit:

  1. Set a cookie in the user's browser to keep track of the posts they've seen.
  2. Pull data out of that cookie to get a list of post IDs
  3. Add this to the array with the $featured_post_id to pass it in the post__not_in parameter

I won't deal with the first two issues ... setting and reading cookies isn't a WordPress question and you can easily find that information with a simple Google search.

But for the rest of it, your code will look something like this:

// get_previously_viewed_posts() will retrieve a list of viewed post IDs from a user cookie.
$posts_to_exclude = get_previously_viewed_posts();

// Add the current post ID to the array of posts to ignore.
$posts_to_exclude[] = get_the_ID();

$args = array(
    'cat'            => '9', 
    'posts_per_page' => 5, 
    'post__not_in'   => $posts_to_exclude

$my_query = new WP_Query( $args );

if ( $my_query->have_posts() ) : while ( $my-query->have_posts() ) : $my-query->the_post();

    // the loop

endwhile; endif;
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.