Array as 'key' in WP_Query

时间:2016-09-28 作者:Joe Turner

第一个查询单独运行良好。凭证有日期、开始时间和结束时间。我正在抓取当前时间(例如11:10),并试图显示$time_now 介于start_timeend_time. 是否无法将数组传递到key?

$today = date(\'l\');
$time_now = date(\'H:i\');

    $args = array (
        \'post_type\'              => array( \'voucher\' ),
        \'meta_query\'             => array(
            array(
                \'key\'       => \'_voucher_days_available\',
                \'value\'     => $today,
                \'compare\'   => \'LIKE\',
                \'type\'      => \'CHAR\',
            ),
            array(
                \'key\' => array(\'_voucher_start_time\',\'_voucher_end_time\'),
                \'value\' => $time_now,
                \'compare\' => \'BETWEEN\',
                ),
        ),
    );

2 个回复
SO网友:cjbj

除了“AhmedMahdi”提出的解决您问题的建议之外,以下是您问题的狭义答案:不,key 不能是数组。正如您在specs of WP_Meta_Query key 必须是字符串。

SO网友:Ahmed Fouad

你能试试下面的吗$args

$today = date(\'l\');
$time_now = date(\'H:i\');

$args = array (
    \'post_type\'              => array( \'voucher\' ),
    \'meta_query\'             => array(
            \'relation\'      => \'AND\',
            array(
                \'key\'       => \'_voucher_days_available\',
                \'value\'     => $today,
                \'compare\'   => \'LIKE\',
                \'type\'      => \'CHAR\',
            ),
            array(
                \'key\'       => \'_voucher_start_time\',
                \'value\'     => $time_now,
                \'compare\'   => \'>=\',
                \'type\'      => \'date\'
            ),
            array(
                \'key\'       => \'_voucher_end_time\',
                \'value\'     => $time_now,
                \'compare\'   => \'<=\',
                \'type\'      => \'date\'
            ),
    ),
);

相关推荐

WordPress Custom Post Loop

我正在尝试循环浏览自定义WordPress帖子,遇到了一个问题,比如我添加了自定义字段并想在中显示它<li> 使用循环。我成功地完成了操作,但数据/链接/类别正在重复,如果类别与以下内容相同,我希望只显示一次:如果我有2篇带有data1类别的帖子,那么链接将只显示data1once 但我有2个不同类别的帖子,然后它会分别显示每个帖子。Sample Code:<ul class="filter filter-top"> <li cla