我正在尝试对热门帖子进行排序,以便显示上周访问量最大的帖子,但没有成功。有人知道为什么它不起作用吗?
<?php
$popularpost = new WP_Query( array (
\'posts_per_page\' => 5,
\'ignore_sticky_posts\' => 1,
\'meta_key\' => \'sw_post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
\'date_query\' => array (
array (
\'year\' => date( \'Y\' ),
\'week\' => date( \'W\' ),
),
),
) );
while( $popularpost->have_posts() ) :
$popularpost->the_post(); ?>
最合适的回答,由SO网友:Abhik 整理而成
使用strtotime
比较日期。
$start = strtotime(\'yesterday\');
$end = strtotime( \'+1 week\',$start);
$args = array(
\'posts_per_page\' => 5,
\'ignore_sticky_posts\' => 1,
\'meta_key\' => \'sw_post_views_count\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
\'date_query\' => array(
\'after\' => $end,
\'before\' => $start,
),
);
$popularpost = new WP_Query( $args );
if ( $popularpost->have_posts() ) {
while ( $popularpost->have_posts() ) {
$popularpost->the_post();
// Do your stuffs
}
}
请注意,这将返回过去7天的帖子,而不是上周。