Dynamically tax_query

时间:2013-04-27 作者:Mike

我举个例子:

$myquery[\'tax_query\'] = array(
    \'relation\' => \'OR\',
    array(
        \'taxonomy\' => \'cities\',
        \'terms\' => \'boston\',
        \'field\' => \'slug\',
        \'operator\' => \'NOT IN\'
    ),
    array(
        \'taxonomy\' => \'cities\',
        \'terms\' => \'chicago\',
        \'field\' => \'slug\',
        \'operator\' => \'NOT IN\'
    ),
);
query_posts($myquery);
但我想做一个普通的表格,因为我有一个带复选框的表格。每个复选框都是我的分类法“城市”的一个术语。这个想法是,如果用户选中任何复选框,它应该显示所有没有标记的帖子。但我希望为tax\\U查询生成与选中复选框一样多的数组。我的意思是:如果用户检查Boston,我会创建:

array(
            \'taxonomy\' => \'cities\',
            \'terms\' => \'boston\',
            \'field\' => \'slug\',
            \'operator\' => \'NOT IN\'
        ),
如果用户检查芝加哥:

array(
            \'taxonomy\' => \'cities\',
            \'terms\' => \'chicago\',
            \'field\' => \'slug\',
            \'operator\' => \'NOT IN\'
        ),
如果用户同时检查这两个选项:

array(
            \'taxonomy\' => \'cities\',
            \'terms\' => \'boston\',
            \'field\' => \'slug\',
            \'operator\' => \'NOT IN\'
        ),
        array(
            \'taxonomy\' => \'cities\',
            \'terms\' => \'chicago\',
            \'field\' => \'slug\',
            \'operator\' => \'NOT IN\'
        ),
我该怎么做?

1 个回复
SO网友:s_ha_dum

只需要一点数组操作。

$def = array(
    \'taxonomy\' => \'cities\',
    \'field\' => \'slug\',
    \'operator\' => \'NOT IN\'
);

$cities = array(
    \'boston\',
    \'chicago\'
);

$args = array(\'relation\' => \'OR\');

foreach ($cities as $c) {
     $args[] = wp_parse_args(array(\'terms\'=>$c),$def);
}
print_r($args); 
The$cities 需要从$_POST$_GET 表单数据,或仅创建$args 首先循环遍历表单数据时使用数组。

结束

相关推荐