我有这样的代码,
$date_arg = array(
'after' => array(
'year' => $num_days_ago['year'],
'month' => $num_days_ago['mon'],
'day' => $num_days_ago['mday'],
),
'inclusive' => false,
);
$query = new WP_Query( array ( 'date_query' => $date_arg, 'post_type' => $post_type ...
这工作正常,但我必须指定一个确切的日期。是否可以使用“timestamp”进行查询。
例如,我想查询24小时内的帖子。我可以通过
获取时间戳$ts = time() - DAY_IN_SECONDS
可以获得$ ts后创建的帖子吗?
答案 0 :(得分:3)
您可以尝试使用date()
和strtotime()
函数构建日期。
$date_query = array(
'after' => date('Y-m-d H:i:s', strtotime('-24 hours'))
);
我还建议您查看这个方便的网站 - http://www.viper007bond.com/tag/wp_date_query/
答案 1 :(得分:0)
恕我直言更好:
function filter_where($where = '') {
$where .= " AND post_date > '" . date('Y-m-d H:i:s', strtotime('-24 hours')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
更一般:
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
// posts in the last x days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";// here 30
return $where;
}
add_filter( 'posts_where', 'filter_where' ); // add the filter before setting object or before loop
$query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' ); // .. and do not forget to remove it
答案 2 :(得分:0)
或者你可以试试这样的东西
SELECT post_title, IF (post_date_gmt < (DATE_SUB(CURDATE(), INTERVAL 24 HOUR)), 'false', 'true') AS A FROM wp_posts
WHERE post_type = "post" AND post_status = "publish"