Display posts from Last 24 hours and order them based on most views

Base on the updated code, the answer to your question, “How can I add filter or query to show the most views post (new posts) of last 24 hours?” is that you can’t do that. Your code is not tracking time at all. There is no way to pull ‘last 24 hours’ if you aren’t tracking times for the post views.

You will need to add another meta_key/meta_value with a timestamp for each post view, or the most recent view at least. If you wrote the code above you should be able to pull that off. If you cribbed it from somewhere… well, I can post something.

Edit:

It appears that you are actually looking for the most viewed posts of those published in the last 24 hours. You have a filter which you posted in a comment below.

function filter_where($where="") { 
  $where .= " AND post_date > '" . date('Y-m-d H:i:s', strtotime('-24 hours')) . "'"; 
  return $where; 
} 
add_filter('posts_where', 'filter_where');

That is about the same as the Codex sample function for filtering by a date range. This doesn’t work?

Leave a Comment