Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Display Last Visited Posts to a User in WordPress

Last updated on by
Special WordPress Hosting offer for WPBeginner Readers
How to Display Last Visited Posts to a User in WordPress

Did you ever wonder how websites like Amazon show you recently viewed items even when you are not logged in? Most online shopping sites use cookies to display recently viewed items to their users. This helps users review their options, compare products, and revisit items they liked. In this article we will show you how to display last visited posts to a user in WordPress. The goal is to keep users interested, remind them of usefulness of your website and build brand recognition.

First thing you need to do is to install and activate the Last Viewed Posts plugin. After activating the plugin, go to Appearance » Widgets. Drag and drop Last Viewed Posts widget to your sidebar and it will immediately start showing posts a user last viewed.

Last viewed posts widget

This plugin uses cookies to keep track of posts a user has viewed on your website. This cookie expires in 360 days, but you can change that by editing the code. Go to Plugins » Editor and select Last Viewed Posts from plugin edit drop down. Find the parameters and edit them to suit your needs:

/* Here are some parameters you may want to change: */
$zg_cookie_expire = 360; // After how many days should the cookie expire? Default is 360.
$zg_number_of_posts = 10; // How many posts should be displayed in the list? Default is 10.
$zg_recognize_pages = true; // Should pages to be recognized and listed? Default is true.

We would recommend 60 days for cookie expiration, and 5 posts to be displayed in the list. If you just want to show the last visited post then set the number of posts to 1. It is also advised that you inform users about cookie usage on your site in your privacy policy page. We hope that this article helps you build a loyal user base. For question and feedback please leave a comment.

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »


  1. kalico says:

    This is a fantastic little gem. Is there a way to make this display other (custom) post types, or history across a multisite network?

  2. Brian says:

    Is there a way to show the whole post (i.e. get_post) instead of just the title? Would you be able to provide the code and the location or where to place it in the plugin code? Thanks!

  3. tony roberts says:

    I have installed the plugin but have trouble installing the widget. Go to Appearance > Widgets > Click on Last Viewed Posts> Click Primary Sidebar>Add Widget, nothing happens. If I drag and drop the Last Viewed Posts still nothing happens.

    Any idea what I’m doing wrong?


  4. RA says:

    Is there a shortcode for this plug in? I would like to insert this within a post.

  5. frebro says:

    This works nicely on my localhost but throws a “Cannot modify header information – headers already sent” error on the production server. Seems like content has already begun to output when you set the cookie.

    I’m using Roots theme and WordPress 3.6. Any suggestions on how to solve this?

  6. musa garip says:

    Hi this plugin is great i have a questions

    can you add a image ( featured image )

  7. David Rwell says:

    Thank you for this lovely little plug-in. It greatly adds to the personalization experience on a site.


  8. Jacopo Tarantino says:

    Is the plugin on github? I’d love to contribute.

    • Editorial Staff says:

      No it is not on Github yet. Please get in touch via the contact form, so we can communicate further :)

  9. Jacopo Tarantino says:

    Wouldn’t it be better to use some kind of override in your own theme or plugin to change the value of those variables? If you edit the plugin directly, when you update the plugin(which you should always do!) you’ll lose those changes. It shouldn’t be too hard to hook in right after the plugin is activated and assign new values to what I’m assuming are global variables.

    • Editorial Staff says:

      Agreed. That is in the list of things to add to that plugin when we create a settings page :)

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.