我目前有一个侧边栏显示最受欢迎的帖子,我想对其添加一个限制。因此,它只会显示过去6个月或30周内或多或少的最受欢迎帖子。
这是当前代码:
$recent_posts = wp_get_recent_posts( array( 'numberposts' => '1' ) );
$thePostID = $recent_posts[0]['ID'];
$popular = new WP_Query(array(
'posts_per_page'=>3,
'post__not_in' => array($thePostID),
'meta_key'=>'popular_posts',
'orderby'=>'meta_value_num',
'order'=>'DESC'
));
while ($popular->have_posts()) : $popular->the_post();
答案 0 :(得分:0)
这有点复杂,但这是一个可行的解决方案:
$recent_posts = wp_get_recent_posts( array( 'numberposts' => '1' ) );
$thePostID = $recent_posts[30]['ID'];
$date1 = explode('-', date('Y-n-j')); // Current date
$date2 = explode('-', date('Y-n-j', strtotime('-6 months'))); // 6 months ago
$popular = new WP_Query(array(
'posts_per_page'=>3,
'post__not_in' => array($thePostID),
'meta_key'=>'popular_posts',
'orderby'=>'meta_value_num',
'order'=>'DESC',
'date_query' => array(
array(
'after' => array(
'year' => $date2[0],
'month' => $date2[1],
'day' => $date2[2],
),
'before' => array(
'year' => $date1[0],
'month' => $date1[1],
'day' => $date1[2],
),
'inclusive' => true,
),
),
));
while ($popular->have_posts()) : $popular->the_post();
解决方案基于本文https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters