如何通过 WordPress 查询获取不包括当前月份的自定义帖子?我创建了一个查询,显示当月发布的帖子...
<?php
$currentYear = date('Y');
$currentMonth = date('n');
$args_current_month = array(
'post_type' => 'post',
'post_status' => array('publish', 'future'),
'posts_per_page' => -1,
'date_query' => array(
array(
'year' => $currentYear,
'month' => $currentMonth
)
)
);
$current_month_query = new WP_Query($args_current_month);
?>
但是我该如何进行相反的查询并排除当月发布的帖子呢?
我尝试了下面的查询,并期望它排除当月的帖子,但它没有......
<?php $currentMonthPosts = array(
'post_status' => array('publish', 'future'),
'date_query' => array(
array(
'year' => $currentYear,
'month' => $currentMonth
)
)
);
$args_future = array(
'post_type' => 'post',
'post_status' => array('future'),
'posts_per_page' => -1,
'post__not_in' => $currentMonthPosts,
);
$future_query = new WP_Query($args_future);`
?>
使用
before
你可以实现这一点,来源:WordPress日期查询
代码:
<?php
$args_current_month = array(
'post_type' => 'post',
'post_status' => array('publish', 'future'),
'posts_per_page' => -1,
'date_query' => array(
array(
'before' => date('F jS, Y', time() - 2592000),
)
)
);
$current_month_query = new WP_Query($args_current_month);
?>
数字
2592000
代表一个月的秒数。