WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to display the posts of last 24 hours and order them by most views.

I call the most views by this code:

if (have_posts()) :
    while (have_posts()) :
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile;

How can I add filter or query to show the most views post (new posts) of last 24 hours?


Here is my post view count code:

// function to count post views.
function setPostViews($postID) {
    $count_key = 'views';
    $count = get_post_meta($postID, $count_key, true);
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        update_post_meta($postID, $count_key, $count);
// function to display number of post views.
function the_views($postID){
    $count_key = 'views';
    $count = get_post_meta($postID, $count_key, true);
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    return $count.' বার';
share|improve this question
What is the plugin or code that is adding the post view counts and does that plugin or code track view times at all? And if it does, how? – s_ha_dum Oct 19 '12 at 14:38
Question now updated with the code – pervez Oct 19 '12 at 15:41
up vote 0 down vote accepted

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.


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?

share|improve this answer
Actually, I want to show only the most viewed post which are posted within last 24 hours. I want to eliminate older post from the most view post list. – pervez Oct 19 '12 at 16:24
That is fine, but you still need a timestamp of some kind. There is no way to pull "most viewed in the last 24 hours" without some way of knowing which posts were viewed in the last 24 hours. That is true whether you are keeping the total long term or deleting them after 24 hours. If you are OK with deleting the 'viewed' data on a fixed cycle you can use wp_cron to delete the 'views' meta_key once daily and you won't have to worry about the timestamp, but you'd have "most viewed today" and not "most viewed is 24 hours", strictly speaking. – s_ha_dum Oct 19 '12 at 17:16
I don't want to delete the older post view data I just want to opt out them from the most viewed post list. I know how to filter the last 24 hours post but don't know how to query among them for most viewed post. I just to want to filter my most viewed list with last 24 hours post filter. Here is the filter code. ` 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'); query_posts($query_string);` – pervez Oct 19 '12 at 17:36
That isn't the most viewed in the last 24 hours. It is the most viewed of those posts published in the last 24 hours. post_date is not updated when someone looks at a post. – s_ha_dum Oct 19 '12 at 19:13
Yes i used that code on my site before but it was filtering all my remaining post. But when you also refer me to this solution I double check the code and figure out that I was forgetting to remove this filter after the loop. Thank you @s_ha_dum – pervez Oct 20 '12 at 2:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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