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

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.

Leave a Comment