Php 在Wordpress中,我可以使用带有时间戳的WP_查询吗?
我有这样的代码Php 在Wordpress中,我可以使用带有时间戳的WP_查询吗?,php,mysql,wordpress,Php,Mysql,Wordpress,我有这样的代码 $date_arg = array( 'after' => array( 'year' => $num_days_ago['year'], 'month' => $num_days_ago['mon'], 'day' => $num_days_ago['mday'], ), 'inclusive' => false,
$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之后创建的帖子?IMHO更喜欢这样:
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
我想最好是这样:
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
您可以尝试使用
date()
和strotime()
函数构造日期
$date_query = array(
'after' => date('Y-m-d H:i:s', strtotime('-24 hours'))
);
我还建议您看看这个方便的网站-您可以尝试使用
date()
和strotime()
函数构造一个日期
$date_query = array(
'after' => date('Y-m-d H:i:s', strtotime('-24 hours'))
);
我还建议你看看这个方便的网站-,或者你可以试试这样的东西
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"
选择post_title,如果(post_date_gmt<(date_SUB(CURDATE(),INTERVAL 24小时)),“false”,“true”)作为来自wp_posts的
其中post_type=“post”和post_status=“publish”
或者你可以试试这样的东西
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"
选择post_title,如果(post_date_gmt<(date_SUB(CURDATE(),INTERVAL 24小时)),“false”,“true”)作为来自wp_posts的
其中post_type=“post”和post_status=“publish”
我认为你必须使用
meta\u query
来实现这一点。我认为你必须使用meta\u query
来实现这一点。