【问题标题】:Wp_query with 2 meta keys and array of meta values具有 2 个元键和元值数组的 Wp_query
【发布时间】:2016-04-17 11:22:15
【问题描述】:

嗨,在我的 post_type=shop 中,我有 2 个元键和值数组

自定义字段

 Name           Values

 cu_status   pending,processing,completed

 cu_date     12-Jan-2016 , 13-Jan-2016, ......  any date in the same format date("d-M-Y")

现在我需要遍历所有带有 cu_status =pending,processing 和 cu_date 在 2016 年 1 月 12 日至 2016 年 4 月 13 日之间的帖子 会查询什么?

我很困惑。为了获得所有状态为待处理的帖子,处理我知道查询

 $args = array(

        'post_type'         => 'shop',

        'post_status'       => 'publish',
        'meta_query' => array(
       array(
           'key' => 'cu_status',
           'value' => array('pending','processing'),
           'compare' => 'IN',
           )
           ),
           'posts_per_page' => -1
                );

请帮助完成查询。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    您需要使用关系运算符,例如

    $args = 数组( 'post_type' => '商店', 'post_status' => '发布', 'meta_query' => 数组( '关系' => '与', 大批( 'key' => 'cu_status', '值' => 数组('待处理','处理'), '比较' => '输入', ) , 大批( 'key' => 'cu_date', '值' => 数组($start, $end), '比较' => '之间', '类型' => '日期' ) ), 'posts_per_page' => -1 );

    还可以使用 compare=>BETWEEN 来获取 2 个日期的差异。 您可能需要稍微调整一下代码,因为我还没有测试过。

    【讨论】:

    • 谢谢。但对我来说它不起作用。 'meta_query' => array('relation' => 'AND', array('key' => 'cu_status', 'value' => array('pending', 'processing'), 'compare' => 'IN ', ), array('relation' => 'AND', array('key' => 'cu_date', 'value' => '04-02-2016', 'compare' => '>=', ) , array( 'key' => 'cu_date', 'value' => '04-03-2016', 'compare' => '
    • 您的代码正在运行。我将日期格式更改为 2016-01-13
    • 没问题。很高兴我能帮忙:)
    • 你能帮忙解决这个问题吗stackoverflow.com/questions/36526268/…
    猜你喜欢
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    相关资源
    最近更新 更多