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.

Hello I want to get all the posts that belong to multiple taxonomies. I saw on codex but it is having only static(2) taxonomies but in my case I have variable number of taxonomies.

On Codex website this is the code...

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'movie_genre',
            'field' => 'slug',
            'terms' => array( 'action', 'comedy' )
        ),
        array(
            'taxonomy' => 'actor',
            'field' => 'id',
            'terms' => array( 103, 115, 206 ),
            'operator' => 'NOT IN'
        )
    )
);
$query = new WP_Query( $args );

but taxonomies can be more than 2 or can be just 1. So How to loop for variable number of taxonomies.

I want like this http://software.pricegrabber.com/office-suites/p/169/

Please help!!

share|improve this question
add comment

2 Answers 2

You can have more than two. You could have as many as you like but you may have performance penalties if you try to use too many. I'd expect this to be true especially if you use an OR relationship.

See the following for a way to create your tax_query array dynamically.

http://wordpress.stackexchange.com/a/97444/21376

To adapt that to your circumstances just associate each term with its taxonomy.

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

$cities = array(
    'boston' => 'tax_city',
    'chicago'  => 'tax_city',
    'texas'  => 'tax_state',
    'california'  => 'tax_state'
);

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

foreach ($cities as $term => $tax) {
  $args[] = wp_parse_args(
    array(
      'taxonomy'=>$tax,
      'terms'=>$term
    ),
    $def
  );
}
print_r($args); 

You could do the same with nested Loops.

$cities = array(
  'tax_city' => array(
    'boston',
    'chicago'
  ),
  'tax_state' => array(
    'texas',
    'california'
   )
);

foreach ($cities as $tax => $terms) {
  foreach ($terms as $term) {
    $args[] = wp_parse_args(
      array(
        'taxonomy'=>$tax,
        'terms'=>$term
      ),
      $def
    );
  }
}
print_r($args);
share|improve this answer
    
no its not same in this example which you give me taxonomy is same but in my case i have both different taxonomy and slug. I want like this software.pricegrabber.com/office-suites/p/169 –  Bhuvnesh May 25 '13 at 15:57
    
Yes, you can do that. The example you posted from the Codex does that. The Question I linked to is not the same. It was written to answer a different question but the principle is the same. You can't just copy and paste it, obviously. Alter the code to fit your needs. –  s_ha_dum May 25 '13 at 16:05
    
Can you please tell me how to do that. I am not able to get it from the answer which is solved in link you posted. –  Bhuvnesh May 26 '13 at 4:30
    
@Bhuvnesh : see the edit –  s_ha_dum May 26 '13 at 21:11
add comment

This article explains how to get posts using multiple taxonomies

share|improve this answer
    
it is static having only 3 taxonomies –  Bhuvnesh May 25 '13 at 16:04
1  
Lone link is considered a poor answer (see FAQ) since it is meaningless by itself and target resource is not guaranteed to be alive in the future. Please try to include at least summary of information you are linking to. –  Rarst May 26 '13 at 20:26
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.