Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have this for example:

$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);

But I would like to make it dinamically, because I have a form with checkboxes. Each checkbox would be a term of my taxonomy "cities". The idea is that if a user select any of the checkboxes, it should show all posts that are NOT in marked terms. But I would like to generate as many arrays for tax_query as checkboxes selected. I mean: If a user check Boston, I create:

array(
            'taxonomy' => 'cities',
            'terms' => 'boston',
            'field' => 'slug',
            'operator' => 'NOT IN'
        ),

If a user check Chicago:

array(
            'taxonomy' => 'cities',
            'terms' => 'chicago',
            'field' => 'slug',
            'operator' => 'NOT IN'
        ),

If a user check both of them:

array(
            'taxonomy' => 'cities',
            'terms' => 'boston',
            'field' => 'slug',
            'operator' => 'NOT IN'
        ),
        array(
            'taxonomy' => 'cities',
            'terms' => 'chicago',
            'field' => 'slug',
            'operator' => 'NOT IN'
        ),

How can I do it?

share|improve this question
add comment

1 Answer 1

Just needs a little array manipulation.

$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 array you need to build from your $_POST or $_GET form data, or just create the $args array as you loop through the form data in the first place.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.