I really need your help my question is this, is it possible to relate to taxonomies? This is My Problem

sample: My post type is "show"

taxonomies and the categories

location (Taxonomy)

  • california
  • alaska
  • new york

genre (Taxonomy) Categories

  • comedy

  • broadway

  • romance

note: each location has 3 genres to choose for example:

california (comedy, broadway, romance)

alaska (comedy, broadway, romance)

newyork (comedy, broadway, romance)

my question is how will make a query like:

displaying all "comedy" shows on "california" location

or something like

displaying all "romance" shows on "alaska" location

if u can send me codes for this ill be gladly appreciate it, i will try to analyze the stuff

again thanks alot



I recently dealt with a similar, although much more complex problem where I had to guess taxonomy names that had no naming convention. I built a whole bunch of abstracted functionality to automatically build my queries for me - your need is simpler though, you might as well do these as the link above explains.

The thing to keep in mind is that there are many nested arrays in this situation. That can be the big gotcha in all this - the arrays can get confusing.

$query = array ( //array level  1
     'tax_query' = array( //array level 2
             'relation' => 'AND' // actually in array level 2!
              array( //array level 3
                  'taxonomy' => 'some_tax',
                  'field' => 'slug', // 'slug' or 'id'
                  'operator' => 'IN', // 'IN', 'NOT' 'IN', or 'AND'
                  'terms' => array ( 'some_terms', 'some_terms2' ) // array level 4

             array( //array level 3
                  'taxonomy' => 'some_other_tax',
                   'field' => 'slug', // 'slug' or 'id'
                  'operator' => 'IN', // 'IN', 'NOT' 'IN', or 'AND'
                  'terms' => 'single_term' // no need for array if only a single term.

Another important thing to look into is the distinction between 'relation' and 'operator'

EDIT: Modified to 'terms' values to show that it does not need to be an array if you dont want to pass multiple terms. Also, when you do pass as an array, you can pass any number of terms you want.

  • Since when has tax_query taken 3 differing operator parameters at once?
    – t31os
    Aug 19 '11 at 12:37
  • Apologies that I was not clear, my intent was to show each possible option. I did the with field. I'll use one, and put the others in a comment
    – eddiemoya
    Aug 19 '11 at 14:21
  • Thanks a lot , i really appreciate it a lot hope there will be more of you out there to help starting developers, more power ;)
    – adolfozen
    Aug 20 '11 at 5:17
  • <?php $query = array( 'tax_query' => array( 'relation' => 'AND', array( 'taxonomy' => 'location', 'field' => 'slug', 'terms' => array( 'california', '' ), ), array( 'taxonomy' => 'genre', 'field' => 'slug', 'terms' => array( 'comedy', '' ), ) ) ); $query = new WP_Query(); ?> Doesnt Show Any Results
    – adolfozen
    Aug 22 '11 at 13:40
  • adolfozen: 'terms' does not have to be an array, but it can be an array if you want to pass more than one term - also if you do use an array, there is no specific number of items you need. I had 2 just as an example. In your situation you should simply have: 'terms' => 'comedy' and 'terms' => 'california'
    – eddiemoya
    Aug 23 '11 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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