获取超过 15 分钟的所有 WP 帖子

ech*_*shu 3 php mysql wordpress

我想过滤 WP_Query 以检索(post_type = shop_order)具有超过 15 分钟的特定状态的所有 woocommerce 订单。

即所有上次修改日期或之前的帖子(现在 - 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;  
Run Code Online (Sandbox Code Playgroud)

但是,返回所有记录,似乎必须修改 $where 查询,我该如何实现?

hel*_*ing 6

需要过滤posts_where吗?你不能只使用日期查询参数吗?在您的情况下,特别是before.

$args = array(
    'date_query' => array(
        array(
            'before'    => '15 minutes ago'
            'inclusive' => true,
        ),
    ),
    'posts_per_page' => -1,
);
$query = new WP_Query( $args );
Run Code Online (Sandbox Code Playgroud)

我现在无法对此进行测试,因此无法验证它是否有效。如果您正在修改档案,那么您绝对应该通过pre_get_posts代替创建新查询来调整查询。