1

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.

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

1 Answer 1

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.

6
  • Wow man. What an answer. The code looks scary to me ;) So if I want only 2 days I put this? $delete_befre_date = new DateTime( "-2 days" ); Is that right?
    – Kilwa
    Commented Jan 15, 2014 at 14:22
  • If you look t the line above that, you can set $days_to_consider = {the number of days you want};
    – TobyG
    Commented Jan 16, 2014 at 14:35
  • Thank you Toby. I have the initial code added in Single.php do I replace the new code with it? or I have to add it in another file?
    – Kilwa
    Commented Jan 16, 2014 at 20:25
  • This should replace what was there before. Note if you had a previous count, this will not take that into consideration & only start counting from now. Also note that I've reformatted the code above as the top lines were not showing as code.
    – TobyG
    Commented Jan 22, 2014 at 14:27
  • Thanks a lot. I tried it. But there is a small problem: The posts are always changing. No idea why. And the number of posts if now 6 instead of 5 even in tabs4.php I have posts => 5. Can I upload my files here please and you can have a look? Website: f1zone.net/news/williams-reveals-pre-launch-image-of-fw36/21761 Thanks a lot!
    – Kilwa
    Commented Jan 23, 2014 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.