META_QUERY返回排除结果

时间:2012-08-08 作者:GhostToast

我在multicheckbox自定义字段中的每个位置都有一系列日间时段。我有一个搜索页面,用户可以在其中选择位置、所需的工作日和活动类型。现在我只有两个示例:一个在星期二开会,另一个不开会。然而,在寻找周二的课程时,两人都出现了。以下是我构建论点的方式:

$searchday = $day; // save this value for the results_array
                        $day = array();
                        for($a=1; $a<=10; $a++) {
                            $day[$a] = array(
                                \'key\' => \'_cmb_\'.$searchday.\'_location_\'.$a,
                                \'value\' => $location,
                                \'compare\' => \'LIKE\'
                                );
                        }

$args = array(\'posts_per_page\' => -1,
                        \'post_type\' => $class_type,
                        \'meta_query\' => array(
                                                \'relation\' => \'OR\',
                                                (($day) ? $day : \'\'),

                                             ),
                        );
这是输出print_r

Array
(
[posts_per_page] => -1
[post_type] => aquatics
[meta_query] => Array
    (
        [relation] => OR
        [0] => Array
            (
                [1] => Array
                    (
                        [key] => _cmb_tuesday_location_1
                        [value] => huntley
                        [compare] => LIKE
                    )

                [2] => Array
                    (
                        [key] => _cmb_tuesday_location_2
                        [value] => huntley
                        [compare] => LIKE
                    )

                [3] => Array
                    (
                        [key] => _cmb_tuesday_location_3
                        [value] => huntley
                        [compare] => LIKE
                    )

                [4] => Array
                    (
                        [key] => _cmb_tuesday_location_4
                        [value] => huntley
                        [compare] => LIKE
                    )

                [5] => Array
                    (
                        [key] => _cmb_tuesday_location_5
                        [value] => huntley
                        [compare] => LIKE
                    )

                [6] => Array
                    (
                        [key] => _cmb_tuesday_location_6
                        [value] => huntley
                        [compare] => LIKE
                    )

                [7] => Array
                    (
                        [key] => _cmb_tuesday_location_7
                        [value] => huntley
                        [compare] => LIKE
                    )

                [8] => Array
                    (
                        [key] => _cmb_tuesday_location_8
                        [value] => huntley
                        [compare] => LIKE
                    )

                [9] => Array
                    (
                        [key] => _cmb_tuesday_location_9
                        [value] => huntley
                        [compare] => LIKE
                    )

                [10] => Array
                    (
                        [key] => _cmb_tuesday_location_10
                        [value] => huntley
                        [compare] => LIKE
                    )

            )

    )

)
我不知道我做错了什么。我甚至检查了数据库,以确保这个类(不应该显示)不包含meta\\u key“\\u cmb\\u tuesday\\u location\\u 1”的值。。2、3等-至10。没有钥匙意味着huntley 找不到值。感谢阅读。

1 个回复
最合适的回答,由SO网友:pcarvalho 整理而成

我不确定这一点,但我认为嵌套数组太多了。

你应该得到

[meta_query] => Array
(
    [relation] => OR
    [0] => Array
                (
                    [key] => _cmb_tuesday_location_1
                    [value] => huntley
                    [compare] => LIKE
                )

    [1] => Array
                (
                    [key] => _cmb_tuesday_location_2
                    [value] => huntley
                    [compare] => LIKE
                )
可以将条件直接添加到右侧数组:

                    [...]
                    $metaq = array();
                    $metaq[relation] = \'OR\'
                    for($a=1; $a<=10; $a++) {
                        $metaq[$a] = array(
                            \'key\' => \'_cmb_\'.$searchday.\'_location_\'.$a,
                            \'value\' => $location,
                            \'compare\' => \'LIKE\'
                            );
                    }
然后

$args = array(\'posts_per_page\' => -1,
          \'post_type\' => $class_type,
          \'meta_query\' => $metaq
);
注意:对不起,我还没有测试过这个

结束

相关推荐

如何在wp-Query中比较不同的时间戳以获取事件自定义帖子类型?

我有一个带有自定义元数据库的自定义帖子类型(as seen on wptheming.com) 并且希望进行查询以显示即将发生的事件,例如在侧边栏中,过去的日期被忽略。但我需要一个当前时间的值来和事件开始时间进行比较。现在我从current_time(\'mysql\') 看起来是这样的:2012-02-16 13:15:31元的开始时间如下所示:201108121100如何使这两个日期具有可比性?我可以在查询中执行吗?或者是否有其他解决方案?以下是我到目前为止的查询(值留空,时间戳回显):<?ph