这是我的查询
$expiring_ads = new WP_Query( array(
'post__in' => get_option('sticky_posts'),
'post_type' => 'post',
'posts_per_page' => 5,
'no_found_rows' => true,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'expire_date',
'value' => date("Y-m-d H:i:s"),
'compare' => '>=',
'type' => 'DATETIME'
)
)
));
我的代码显示所有日期到期的所有帖子。如何只显示24小时后到期?
答案 0 :(得分:1)
只需添加add_filter
即可通过posts_where
添加条件,以检查数据库中的expire_date
,如下所示:
// Create a filtering function to add a WHERE condition to your query like this.
function filter_expire() {
return " AND expire_date <= '" . date('Y-m-d H:i:s', strtotime('1 day')) . "'";
}
// Add the filter to the query.
add_filter('posts_where', 'filter_expire');
// Run the query.
$expiring_ads = new WP_Query( array(
'post__in' => get_option('sticky_posts'),
'post_type' => 'post',
'posts_per_page' => 5,
'no_found_rows' => true,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'expire_date',
'value' => date("Y-m-d H:i:s"),
'compare' => '>=',
'type' => 'DATETIME'
)
)
));
编辑:好的,已经意识到您正在使用元帖子信息。所以试试这个:
// Run the query.
$expire_window = date('Y-m-d H:i:s', strtotime('1 day'));
$expiring_ads = new WP_Query( array(
'post__in' => get_option('sticky_posts'),
'post_type' => 'post',
'posts_per_page' => 5,
'no_found_rows' => true,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'expire_date',
'value' => $expire_window,
'compare' => '<='
),
array(
'key' => 'expire_date',
'value' => date("Y-m-d H:i:s"),
'compare' => '>=',
'type' => 'DATETIME'
)
)
));