wordpress新WP_Query之间无法正常工作

时间:2017-08-31 07:20:35

标签: php wordpress

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

这是我的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的结果显示两个整数的过滤外部。哪里出错了?

3 个答案:

答案 0 :(得分:1)

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

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

您可以在Codex

中看到这一点

答案 1 :(得分:1)

试一试:

//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);

答案 2 :(得分:0)

我的问题的答案是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);