[英]In Wordpress, can I use WP_Query with timestamp?
I have code like this, 我有这样的代码,
$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 ...
This is working fine but I have to specify a exact date. 这个工作正常,但我必须指定一个确切的日期。 Is it possible to use "timestamp" to make the query.
是否可以使用“时间戳”进行查询。
For example, I want to query posts that is within 24 hours. 例如,我要查询24小时内的帖子。 I can get the timestamp by
我可以通过获得时间戳
$ts = time() - DAY_IN_SECONDS
It is possible to get the posts that is created after $ts? 是否有可能获得在$ ts之后创建的帖子?
You could try constructing a date using the date()
and strtotime()
functions. 您可以尝试使用
date()
和strtotime()
函数构造日期。
$date_query = array(
'after' => date('Y-m-d H:i:s', strtotime('-24 hours'))
);
I'd also recommend taking a look at this handy website - http://www.viper007bond.com/tag/wp_date_query/ 我还建议您浏览这个方便的网站-http: //www.viper007bond.com/tag/wp_date_query/
IMHO better like so : 恕我直言,像这样更好:
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');
and more general : 更一般的:
// 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
Or you could try something like this 或者你可以尝试这样的事情
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"
http://www.w3schools.com/sql/func_date_sub.asp http://www.w3schools.com/sql/func_date_sub.asp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.