[英]In Wordpress, can I use WP_Query with timestamp?
我有这样的代码,
$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 ...
这个工作正常,但我必须指定一个确切的日期。 是否可以使用“时间戳”进行查询。
例如,我要查询24小时内的帖子。 我可以通过获得时间戳
$ts = time() - DAY_IN_SECONDS
是否有可能获得在$ ts之后创建的帖子?
您可以尝试使用date()
和strtotime()
函数构造日期。
$date_query = array(
'after' => date('Y-m-d H:i:s', strtotime('-24 hours'))
);
我还建议您浏览这个方便的网站-http: //www.viper007bond.com/tag/wp_date_query/
恕我直言,像这样更好:
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
或者你可以尝试这样的事情
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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.