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.
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…
Then to query the posts…
To change the number of days kept, change the value of
$days_to_consider
in the first block of code.