I am working on a project that showcases a number of products, so I'd like to add a "recently viewed product" selection that keeps track of the last 5 or so viewed products or posts.

I saw this plugin but its 2 years old and as of now doesn't look to be supported.

Any ideas?

  • Sorry, but plugin recommendations are off topic here. Instead, try wordpress.org/support
    – shea
    Dec 19 '12 at 6:26
  • You could write your own php function to ride along in your site's header and pass along session/cookie data to each page. Then intercept that on sidebar or wherever, so it could interpret that (just capture the post IDs) as your recently viewed items.
    – GhostToast
    Dec 19 '12 at 17:52
  • i dont this its a plugin question @bungeshea - he just said he looked for a ready solution but found an old one. Also i find this an intresting question ;) (in my point of view)... johnathon... are you talking about looged in users or visitors? ... GhostToast i would love to see an basic example for your solution.
    – Sagive SEO
    Dec 20 '12 at 3:51
  • @SagiveSEO The question had the plugin-recommendation tag
    – shea
    Dec 20 '12 at 4:00
  • Indeed it did have the plugin recommendation tag, was a noob mistake. Was more looking towards plugin advice the tag fit considering the situation. I surprisingly got the old plugin working and set up to handle custom posts, and as of now its working. Its been a week or so, but i believe the old plugin worked - which is great and saved me tons of time. For the record this was set up for all users, and will be used to show previously viewed products - a nice easy way to compare multiple products. Jan 3 '13 at 19:37

You can add a timestamp to your post meta each time a product is viewed, then query the five most recently viewed products.

Assuming you are using a custom post type named 'product', add the following inside the loop of your single-product.php template file:

if (get_post_type( $post->ID ) == 'product' )
    update_post_meta( $post->ID, '_last_viewed', current_time('mysql') );

To display the five most recently viewed products:

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 5,
    'meta_key' => '_last_viewed',
    'orderby' => 'meta_value',
    'order' => 'DESC'
query_posts( $args ); ?>
<?php if( have_posts() ) : ?>
    <?php while( have_posts() ) : the_post(); ?>
        <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    <?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
  • 1
    This would show the same products to everybody.
    – Ciprian
    Feb 20 '20 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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