I insert today 20 post for example.

And i need to get top post that show today from today post.

I try this code but its show me top post view for all time. But I need only for today post.

$day = date('d');
$month = date('m');
$year = date('Y'); 
    array(  'post_type' => 'post',
            'order'     => 'ASC',
            'meta_key' => 'post_views_count',
            'orderby'   => 'meta_value', 
            'year' => '$year',
            'monthnum' => '$month',
            'day' => '$day',
  • You should never use query_posts(), it will do funky things to your blog. Instead look into the pre_get_posts action hook, the get_posts() function or the WP_Query class. – David Gard Feb 21 '15 at 22:27
  • ^^ That said, can I assume that the vale of the post_views_count meta key is updated every time a post is viewed? – David Gard Feb 21 '15 at 22:28

As your question does not state, I'm assuming that this is not your main query, but rather a stand alone that tells the visitor the most viewed page of the day.

Therefore I believe that an instance of the WP_Query class is what you need.

/** Query the posts to find the one with the most views from today */
$today = getdate();
$args = array(
    'meta_key'          => 'post_views_count',
    'orderby'           => 'meta_value_num',
    'posts_per_page'    => 1,
    'post_type'         => 'post',
    'post_status'       => 'publish',
    'date_query'        => array(
            'year'  => $today['year'],
            'month' => $today['mon'],
            'day'   => $today['mday']
$my_query = new WP_Query($args);
echo '<pre>$my_query: '; print_r($my_query); echo '</pre>';

/** Check for results and output them */
if($my_query->have_posts()) : while($my_query->have_posts()) : $my_query->the_post();

        /** Your code goes here, inside The Loop */
        echo '<pre>' . get_the_title() . ' (' . get_post_meta(get_the_ID(), 'post_views_count', true) . ')' . '</pre>';

|improve this answer|||||
  • hello ,, I try your code but its not return me any post, – Kotsh GFX Feb 22 '15 at 7:06
  • Are you absolutely sure that you have posts with today's date? – David Gard Feb 22 '15 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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