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?


This plugin may fulfill your requirements.



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 to develop this functionality yourself, download the plugin and see the source files. WordPress is Open Source!


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

 * 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();


        // 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.


  • 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

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.