$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How get top post view for today for today post|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How get top post view for today for today post

matteradmin10PV0评论

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'); 
query_posts(
    array(  'post_type' => 'post',
            'order'     => 'ASC',
            'meta_key' => 'post_views_count',
            'orderby'   => 'meta_value', 
            'year' => '$year',
            'monthnum' => '$month',
            'day' => '$day',
    )
);

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'); 
query_posts(
    array(  'post_type' => 'post',
            'order'     => 'ASC',
            'meta_key' => 'post_views_count',
            'orderby'   => 'meta_value', 
            'year' => '$year',
            'monthnum' => '$month',
            'day' => '$day',
    )
);
Share Improve this question asked Feb 21, 2015 at 21:49 Kotsh GFXKotsh GFX 436 bronze badges 2
  • 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 Commented Feb 21, 2015 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 Commented Feb 21, 2015 at 22:28
Add a comment  | 

1 Answer 1

Reset to default 1

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(
        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>';

    endwhile;
    wp_reset_postdata();
endif;

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far