Show number of views in the last 48 hours

I have now the most popular views on the posts I have. But I want to show the most viewed in the last 48 hours. How can I achieve that?

Someone helped me and I placed this in single.php file.

Read More

$post_id = get_the_ID();
$post_view_count = get_post_meta($post_id, ‘view_count’, true);

    if( $post_view_count=='' ) {                
            update_post_meta($post_id, 'view_count', 1);                
    }
    else{
            $post_view_count = intval($post_view_count);
            ++$post_view_count;
            update_post_meta($post_id, 'view_count', $post_view_count);
    } 

And I have this in tabs4.php to show the 4 tabs on my website:

$args_popular = array (
            'posts_per_page'         => '5',
            'order'                  => 'DESC',
            'orderby'                => 'meta_value_num',
            'meta_key'               => 'view_count',
            'day'                    => '2',
            'cache_results'          => true,

But current day => 2 is not working.

Thanks a lot.

Related posts

1 comment

  1. There’s no way to identify when each of the page view’s recorded happened. It’s only logging the page view, not when it happened.

    To work around this you could keep a post_meta entry for each view, but this could create a lot of entries on popular sites. Alternatively you could keep daily counts & delete any that were over 2 days old (if you’re only interested in 48 hours), as well as maintaining the running total count.

    Here’s a suggested solution for the daily count method…

    To add the daily counts…

    $post_id               = get_the_ID();
    $post_view_count       = get_post_meta( $post_id, 'view_count', true );
    $post_daily_view_count = get_post_meta( $post_id, 'view_daily_counts', true );
    $post_recent_count     = get_post_meta( $post_id, 'view_recent_count', true );
    
    if ( $post_view_count == '' ) {
        update_post_meta( $post_id, 'view_count', 1 );
    } else {
        $post_view_count = intval( $post_view_count );
        ++$post_view_count;
        update_post_meta( $post_id, 'view_count', $post_view_count );
    }
    
    $today             = new DateTime;
    $days_to_consider  = 2; // How many days to consider when looking for recent views?
    $delete_befre_date = new DateTime( "-{$days_to_consider} days" );
    
    // Create the meta entry if doesn't already exist
    if ( $post_daily_view_count == '' ) {
        $post_daily_view_count = array( $today->format( 'Y-m-d' ) => 1 );
    
    // Otherwise, update the existing one
    } else { // if ( $post_daily_view_count == '' )
    
        // Update the entry for today
        $post_daily_view_count[$today->format( 'Y-m-d' )] = intval( $post_daily_view_count[$today->format( 'Y-m-d' )] );
        ++$post_daily_view_count[$today->format( 'Y-m-d' )];
    
    } // if ( $post_daily_view_count == '' )
    
    // Calculate the recent total, removing entries older than X days from the recent counts
    $recent_count = 0;
    foreach ( array_keys( $post_daily_view_count ) as $date ) {
        if ( $date < $delete_befre_date->format( 'Y-m-d' ) ) {
            unset( $post_daily_view_count[$date] );
        } else {
            $recent_count = $recent_count + $post_daily_view_count[$date];
        }
    }
    
    // And save the update meta
    update_post_meta( $post_id, 'view_daily_counts', $post_daily_view_count );
    update_post_meta( $post_id, 'view_recent_count', $recent_count );
    

    Then to query the posts…

    $args_popular = array(
        'posts_per_page' => '5',
        'order'          => 'DESC',
        'orderby'        => 'meta_value_num',
        'meta_key'       => 'view_recent_count',
        'cache_results'  => true,
    );
    

    To change the number of days kept, change the value of $days_to_consider in the first block of code.

Comments are closed.