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
|
1 Answer
Reset to default 1As 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;
query_posts()
, it will do funky things to your blog. Instead look into thepre_get_posts
action hook, theget_posts()
function or theWP_Query
class. – David Gard Commented Feb 21, 2015 at 22:27post_views_count
meta key is updated every time a post is viewed? – David Gard Commented Feb 21, 2015 at 22:28