Leave a Reply

1 comment

  1. A very simplistic method of achieving this would be as follows;

    add_action('template_redirect', 'recently_read');
    function recently_read(){
    
        global $post;
    
        //only run this function for the posts post_type
        if ( !is_single() ) 
            return;
    
        //get the current time in the format of: 2013-02-22 11:55:51 
        $timestamp = current_time('mysql'); 
    
        //add the $timestamp variable to the meta_key for this post
        update_post_meta($post->ID, 'reading_this', $timestamp);
    
    
    }
    

    Note: The above function goes into your theme functions.php file and is hooked onto the template_redirect action which is fired just before displaying the page to the user. At this point, behind the scenes, our recently_read function will fire and add a time stamp under the key reading_this (name it to your liking). This will occur each time this or any post is viewed. If a time stamp already exists for a given post, it will then update the time stamp accordingly.

    Then to retrieve a list of posts with this key you would do the following in your theme files where you want those posts to display;

    $recently_read = get_posts( 
        array(
            'posts_per_page'  => 5,       //how many posts we want to show at most
            'meta_key' => 'reading_this', //get posts by our meta key
            'orderby' => 'meta_value',    //order posts by the value stored in the key
            'order' => 'DESC'             //order posts in descending order (newest to oldest)
       )
    ); 
    
    foreach ($recently_read as $read) {
        echo '<a href="'. get_permalink($read->ID) . '">'. $read->post_title .'</a>';
    }
    

    The above could be improved a number of ways but this will definitely get you started.

    Note: This is void of any logic that determines if the post was viewed in the last X minutes. For that you can apply your own logic to the meta_key named reading_this and manipulate the data to your liking.