Querying Multiple Custom Taxonomy Terms

I created a new taxonomy called ‘country’ and have two terms items inside it: ‘USA’ and ‘Canada’. How do I go about querying this info? I have a filter page with two checkboxes:

<input type="checkbox" name="usa" value="USA" /> USA
<input type="checkbox" name="canada" value="Canada" /> Canada

How do I make the query show multiple results in case I have both boxes checked? I tried an array, but it didn’t work.

<?php query_posts( array('country' => array($_POST['usa'], $_POST['canada'])) ); ?>

Related posts

Leave a Reply

2 comments

  1. You’ll want to use the tax_query argument for query_posts/WP_Query.

    query_posts( array(
      "tax_query" => array(
        array(
          "taxonomy" => "country",
          "field" => "slug",
          "terms" => array( "usa", "canada" )
        )
      )
    ) );
    

    The tax_query argument is an array, so you can query multiple taxonomies.

  2. You’ll have to clarify your query, but these might also be helpful:

    the_terms Displays the terms of a custom taxonomy: http://codex.wordpress.org/Function_Reference/the_terms

    get_terms Retrieve the terms in taxonomy or list of taxonomies:
    http://codex.wordpress.org/Function_Reference/get_terms

    I just found this after days of searching for this solution which may also be what you’re after.
    https://stackoverflow.com/questions/3848608/iterate-through-custom-post-type-by-custom-taxonomy-type-ordering-wordpress-pos