Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

http://wordpress.org/extend/plugins/recently-viewed-posts/

Any ideas?

share|improve this question
    
Sorry, but plugin recommendations are off topic here. Instead, try wordpress.org/support –  bungeshea 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 –  bungeshea 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. –  Johnathon Mathews Jan 3 '13 at 19:37
show 1 more comment

1 Answer

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:

<?php 
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:

<?php
$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(); ?>
share|improve this answer
add comment

Your Answer

 
discard

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.