How to pass URL parameters for advanced taxonomy queries with multiple terms for one custom taxonomy

This answer provides the code for a query that returns an OR match on a taxonomy term:

global $query_string;
$args['tax_query'] = array(
    array(
         'taxonomy' => 'status'
        ,'terms'    => array( 'available', 'pending' ) // change to "sold" for 2nd query
        ,'field'    => 'slug'
    ),
);

I’d like to have my taxonomy template retrieve the term values for the array ( ,’terms’ => array( ‘available’, ‘pending’ ) ) from the URL, the same as for the single term.

Read More

The point is to allow taxonomy terms to change over time, without having to hard-code page templates for particular combinations of interest, so that, if there’s a better way, please suggest it.

Thanks to this post I have permalinks mapping to template files as follows:

example.com/cptslugs                maps to my archive-cptname.php template. 
example.com/cptslug/cpt-post        maps to my single-cptname.php template. 
example.com/cptslugs/ctxslug/term   maps to my taxonomy-ctxname.php template. 

I’d like to have something like:

example.com/cptslugs/ctxslug/term1/term2/

map to my taxonomy-ctxname.php template such that get_query_var( ‘term’ ) returns an array of terms (term1,term2) from the url.

but if I could get

example.com/cptslugs/ctxslug/?terms=term1,term2

to map to my taxonomy-ctxname.php template such that get_query_var( ‘term’ ) returns an array of terms (term1,term2) from the url, perhaps via a rewrite rule and a filter, that would be ok, too.

But either way, the idea is that the template file should work, whether it’s passed one term or an array of terms.

UPDATE

Ok – @scribu to the rescue! — maybe. Kept searching, and this is what I found. Thought I had it all figured, but I can’t get it to work.

?tax1=term1,term2&tax2=term2+term3

maps to:

query_posts( array(
  'tax1' => 'term1,term2',
  'tax2' => 'term3+term4'
) );

which further maps to:

query_posts( array(
  'tax_query' => array(
    array(
      'taxonomy' => 'tax1',
      'field' => 'slug',
      'terms' => array('term1', 'term2'),
      'operator' => 'OR'
    ),
    array(
      'taxonomy' => 'tax2',
      'field' => 'slug',
      'terms' => array('term3', 'term4'),
      'operator' => 'AND'
    ),
) );

example.com/?ctxname=term1,term2

renders the front page template. Revision: it renders taxonomy-ctxname.php when you spell ctxname correctly. Imaging that!

example.com/?cptslug/ctxslug=term1,term2

(taxonomy is registered with a slug of cptslug/ctxslug) also renders the front page template.

example.com/cptslug/?ctxslug=term1+term2

renders the archive-ctpname.php template.

How do I get a URL that renders the taxonomy-ctxname.php or the taxonomy.php template?

Then, once I get that down – suggestions for pretty urls are welcome but not absolutely necessary.

UPDATE TWO

example.com/?ctxname=term1,term2

and

example.com/ctpslugs/ctxslug/term1,term2/

Both render taxonomy-ctxname.php as desired. See details in my answer, posted below.

Related posts

Leave a Reply

1 comment

  1. After adding

     print_r($wp_query); 

    to my template and examining the results, I have discovered URL formats that work. I wrote, in my question, that the following format doesn’t work — in fact, it does – if you spell your custom taxonomy name correctly.

    example.com/?ctxname=term1+term2

    Pretty URLs with the ‘+’ and ‘,’ operators (indicating AND and OR respectively) are only recognized when not URL-encoded.

    example.com/cptslugs/ctxslug/term1,term2/ 

    produces the following WP_Tax_Query Object

    [tax_query] => WP_Tax_Query Object
          (
              [queries] => Array
                  (
                      [0] => Array
                          (
                              [taxonomy] => announcements_cats
                              [terms] => Array
                                  (
                                      [0] => term1
                                      [1] => term2
                                  )
    
                              [include_children] => 1
                              [field] => slug
                              [operator] => IN
                          )
    
                      [1] => Array
                          (
                              [taxonomy] => category
                              [terms] => Array
                                  (
                                      [0] => 1
                                  )
    
                              [include_children] => 
                              [field] => term_id
                              [operator] => NOT IN
                          )
    
                  )
    
              [relation] => AND
          )
    

    While

    example.com/cptslugs/ctxslug/term1+term2/

    produces the following WP_Tax_Query Object

    [tax_query] => WP_Tax_Query Object
          (
              [queries] => Array
                  (
                      [0] => Array
                          (
                              [taxonomy] => ctxname
                              [terms] => Array
                                  (
                                      [0] => term1
                                  )
    
                              [include_children] => 1
                              [field] => slug
                              [operator] => IN
                          )
    
                      [1] => Array
                          (
                              [taxonomy] => ctxname
                              [terms] => Array
                                  (
                                      [0] => term2
                                  )
    
                              [include_children] => 1
                              [field] => slug
                              [operator] => IN
                          )
    
                      [2] => Array
                          (
                              [taxonomy] => category
                              [terms] => Array
                                  (
                                      [0] => 1
                                  )
    
                              [include_children] => 
                              [field] => term_id
                              [operator] => NOT IN
                          )
    
                  )
    
              [relation] => AND
          )
    

    P.S.: According to this: the ‘+’ and the ‘,’ are ‘Reserved Characters’ and can be used in the path, parameters and fragment of a URI, but may not appear in other parts of a URI. I guess it’s OK after all. Just seems ‘wrong’ somehow, though.