【问题标题】:wordpress new WP_Query between not workingwordpress new WP_Query 之间不工作
【发布时间】:2017-08-31 18:59:59
【问题描述】:

如何让它在两个整数之间进行过滤?

这是我的 PHP

$filter = array(
    'post_type'     => 'request',
    'post_status'   => 'publish',
    'posts_per_page' => -1,
    'meta_query' => array(
        'key' => 'first_posting_date',
        'value' => array('20170826','20170829'), //already tried to remove the qoutes on numbers
        'compare' => 'BETWEEN'
        )
    );
    $posts = new WP_Query($filter);
    print_r($posts);

print_r 的结果显示了过滤后的两个整数的外部。我哪里出错了?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    可以将多个元值与 BETWEEN 与数组值进行比较:

    'meta_query' => array(
        array(
            'key'     => 'first_posting_date',
            'value' => array('20170826','20170829'),
            'type'    => 'numeric',
            'compare' => 'BETWEEN',
        ),
       ),
    

    您可以在 Codex 中看到这一点

    【讨论】:

    • 使用 'value' => 数组(20170826,20170829),不带引号
    • 我只是想念'type' => 'numeric',
    【解决方案2】:

    试试看:

    //Date
    $start = '2017-08-26';
    $end = '2017-08-29';
    $filter = array(
        'post_type' => 'request',
        'post_status'   => 'publish',
        'posts_per_page' => -1,
        'meta_key' => 'first_posting_date',
        'meta_query' => array(
            array(
                'key' => 'first_posting_date',
                'value' => array($start, $end),
                'compare' => 'BETWEEN',
                'type' => 'DATE'
            )
        )
    );
    
    // Make the query
    $posts = new WP_Query($filter);
    print_r($posts);
    

    【讨论】:

      【解决方案3】:

      我的问题的答案是relation 并将meta_query 的数组放到其他数组中

      应该是这样的

      $filter = array(
          'post_type'     => 'request',
          'post_status'   => 'publish',
          'posts_per_page' => -1,
          'meta_query' => array(
               "relation" => "AND",
               array(
                  'key' => 'first_posting_date',
                  'value' => array('20170826','20170829'),
                  'compare' => 'BETWEEN'
                )
             )
          );
          $posts = new WP_Query($filter);
          print_r($posts);
      

      【讨论】:

        猜你喜欢
        • 2012-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-19
        • 2019-08-29
        • 1970-01-01
        相关资源
        最近更新 更多