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 have a lot of video posts on my site and I'm looking to show the user a list of posts they've recently viewed. I'm guessing the best route is to just add the post id to an array in their usermeta when they visit a post but I'm wondering how can I do that and still be able to show them a chronological order of posts in which they viewed them. And also I wanna clip the number of posts in the array keeping it at 20 posts or so that the array isn't out of control. Any ideas?

share|improve this question

1 Answer 1

This plugin may fulfill your requirements.

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

Usage

get_recently_viewed_posts( $max_shown = 10 ) returns a string of li's. recently_viewed_posts( $max_shown = 10 ) prints a div

If you are interested in learning how develop these functionalities yourself, download the plugin and see the source files. WordPress is Open Source!

Update:

The above plugin did not worked. The following approach will help to get the recently viewed video ids.

<?php        
/*
 * Plugin Name: WPSE_63266_Recently_Viewed
 */
function wpse_63266_update_recently_viewed(){

    /**
     *  If is admin or isn't single, then return.
     *  To get only singular video posts use; if(!is_singular('videos')) return;
     */
    if(is_admin() || !is_single()) return;

    global $post;

    // Get the current post id.
    $current_post_id = get_the_ID();

    if(is_user_logged_in()){

        // Store recently viewed post ids in user meta.
        $recenty_viewed = get_user_meta(get_current_user_id(), 'recently_viewed', true);
        if( '' == $recenty_viewed ){
            $recenty_viewed = array();            
        }

        // Prepend id to the beginning of recently viewed id array.(http://php.net/manual/en/function.array-unshift.php)
        array_unshift($recenty_viewed, $current_post_id);        

        // Keep the recently viewed items at 5. (http://www.php.net/manual/en/function.array-slice.php)
        $recenty_viewed = array_slice($recenty_viewed, 0, 5); // Extract a slice of the array

        // Update the user meta with new value.
        update_user_meta(get_current_user_id(), 'recently_viewed', $recenty_viewed);

    } else {

    /**
     * For non-logged in users you can use the same procedure as above
     * using get_option() and update_option()
     */

    }
}
add_action('wp_footer', 'wpse_63266_update_recently_viewed');

function wpse_63266_show_recently_viewed(){

    $recenty_viewed = get_user_meta(get_current_user_id(), 'recently_viewed', true);
    echo '<pre>'; print_r($recenty_viewed); echo '</pre>';
}
add_action('wpse_63266_recently_viewed', 'wpse_63266_show_recently_viewed');
?>

do action() in your template to see the change of array values (video ids) while you are browsing through the posts.

do_action('wpse_63266_recently_viewed');

share|improve this answer
1  
not sure recommending plugins that haven't been updated in 2 years is good form but I'll check it out. –  Pollux Khafra Aug 28 '12 at 3:15

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.