【问题标题】:Get All WP Posts older than 15 minutes获取所有超过 15 分钟的 WP 帖子
【发布时间】:2016-01-15 02:30:11
【问题描述】:

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

即上次修改的所有帖子(现在 - 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 查询,我该如何实现?

【问题讨论】:

  • 你能告诉我们你在哪里声明$loop吗?
  • 已更新,就在 while 循环上方

标签: php mysql wordpress


【解决方案1】:

你需要过滤posts_where吗?你不能用date query parameters吗?在您的情况下,特别是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 调整查询,而不是创建新查询。

【讨论】:

    【解决方案2】:

    尝试将 date('Y-m-d', strtotime('INTERVAL 15 MINUTE')) 更改为 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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多