我在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
找不到值。感谢阅读。
最合适的回答,由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
);
注意:对不起,我还没有测试过这个