admin管理员组文章数量:1130349
I'm building a weekly leaderboard and trying to display posts from on the current calendar week Monday to Sunday. I've tried the code below, but it's only retrieving a post from today (Tuesday 1st January) and no posts from the day before (Monday 31st December). Any ideas? Thanks!
$args = array(
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
'post_type' => 'ride', 'posts_per_page' => 99, 'order' => 'DEC',
);
$leaders = new WP_Query( $args );
I'm building a weekly leaderboard and trying to display posts from on the current calendar week Monday to Sunday. I've tried the code below, but it's only retrieving a post from today (Tuesday 1st January) and no posts from the day before (Monday 31st December). Any ideas? Thanks!
$args = array(
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
'post_type' => 'ride', 'posts_per_page' => 99, 'order' => 'DEC',
);
$leaders = new WP_Query( $args );
Share
Improve this question
edited Jan 1, 2019 at 22:01
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Jan 1, 2019 at 20:11
leandaleanda
1377 bronze badges
1 Answer
Reset to default 0I think I know what happens here...
Most probably that Monday is another week - last one in 2018, and it makes sense. Last day of 2018 can’t be the first week of 2019 ;)
So I would query it in a different way:
'date_query' => array(
'after' => '-' . (intval(date('N')) - 1) . 'days',
'inclusive' => true
)
What it does is takes all posts published after given date. And to compute that date we take current day and substract the current day of the week - so we get last Monday...
I'm building a weekly leaderboard and trying to display posts from on the current calendar week Monday to Sunday. I've tried the code below, but it's only retrieving a post from today (Tuesday 1st January) and no posts from the day before (Monday 31st December). Any ideas? Thanks!
$args = array(
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
'post_type' => 'ride', 'posts_per_page' => 99, 'order' => 'DEC',
);
$leaders = new WP_Query( $args );
I'm building a weekly leaderboard and trying to display posts from on the current calendar week Monday to Sunday. I've tried the code below, but it's only retrieving a post from today (Tuesday 1st January) and no posts from the day before (Monday 31st December). Any ideas? Thanks!
$args = array(
'date_query' => array(
array(
'year' => date( 'Y' ),
'week' => date( 'W' ),
),
),
'post_type' => 'ride', 'posts_per_page' => 99, 'order' => 'DEC',
);
$leaders = new WP_Query( $args );
Share
Improve this question
edited Jan 1, 2019 at 22:01
Krzysiek Dróżdż
25.6k9 gold badges53 silver badges74 bronze badges
asked Jan 1, 2019 at 20:11
leandaleanda
1377 bronze badges
1 Answer
Reset to default 0I think I know what happens here...
Most probably that Monday is another week - last one in 2018, and it makes sense. Last day of 2018 can’t be the first week of 2019 ;)
So I would query it in a different way:
'date_query' => array(
'after' => '-' . (intval(date('N')) - 1) . 'days',
'inclusive' => true
)
What it does is takes all posts published after given date. And to compute that date we take current day and substract the current day of the week - so we get last Monday...
本文标签: date queryGet posts only from current calendar week
版权声明:本文标题:date query - Get posts only from current calendar week 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749053468a2308961.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论