我想过滤WP_Query以检索所有具有特定状态且超过 15 分钟的 wooCommerce 订单(post_type = shop_order)
。
即所有上次修改过的帖子(现在 - 15 分钟)//希望我清楚。
以下是我尝试过的
function filter_where($where = '') {
//posts in the last 15 minutes
$where .= " AND post_modified > '" . date('Y-m-d', strtotime('INTERVAL 15 MINUTE')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
$args = array(
'post_type' => 'shop_order',
'post_status' => 'publish',
'posts_per_page' => 10,
'tax_query' => array(
array(
'taxonomy' => 'shop_order_status',
'field' => 'slug',
'terms' => array('completed')
)
)
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
print_r("<pre>");
print_r($order);
print_r("</pre>");
endwhile;
但是,返回所有记录,似乎必须修改$where查询,我该如何实现?
你需要过滤posts_where
吗?不能只使用日期查询参数吗?在您的情况下,特别是before
.
$args = array(
'date_query' => array(
array(
'before' => '15 minutes ago'
'inclusive' => true,
),
),
'posts_per_page' => -1,
);
$query = new WP_Query( $args );
我现在无法对此进行测试,因此无法验证它是否有效。如果要修改存档,则绝对应通过pre_get_posts
调整查询,而不是创建新查询。
试试这个更改日期('Y-m-d', strtotime('INTERVAL 15 MINUTE')) to date('Y-m-d H:i:s', strtotime('-15 MINUTES'))
function filter_where($where = ''){
//posts in the last 15 minutes
$where .= " AND post_modified > '" . date('Y-m-d H:i:s', strtotime('-15 minutes')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');