繁体   English   中英

WordPress之间新的WP_Query之间不起作用

[英]wordpress new WP_Query between not working

如何使其在两个整数之间过滤?

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

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

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

您可以在Codex中看到这一点

试试吧:

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

我的问题的答案是relation ,并将meta_query数组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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM