How to build a multi-taxonomy, multi-term query based on user input

Thanks to Otto‘s excellent post on advanced taxonomy queries, I (mostly) understand how to create multi-taxonomy and multi-term queries. However, what I don’t know is how to build those dynamically based on user input. I’d really like to end up with something like Amazon.com has on their site, with check boxes next to the various terms for a taxonomy in the sidebar (and multiple lists of terms, each with a taxonomy heading). Something like this:

Taxonomy with list of terms and check boxes to select those terms

Read More

How should I go about building something like this? Displaying the content dynamically (content changes when a box is checked) would be great, but I’d certainly settle for having a “submit” button.

Related posts

Leave a Reply

2 comments

  1. I am a little fuzzy on specifics of technical side, but I think general outline would be following:

    1. Interface – you will need to either submit this as form by button or JavaScript.

    2. Query variables – you will need to register custom variable(s) via query_vars filter so your custom data is not discarded from URL.

    3. Query – modify query to make complex query, using custom data submitted.
  2. submit the form with javascript when a box status is changed. Then you can use wp_query to create a new query and use tax_query to display taxonomies that is checked:

    $args = array(
        ... // posttype and such
        'tax_query' => array(),
        ...// other arguments 
    );
    //if post is set
    if(isset($_POST['cats'])){
        //pusch to array
        $args['tax_query'][] = array(
            'taxonomy' => 'the taxonomy',
            'field' => 'id', // get tax by id or slug
            'terms' => $_POST['cats'],
            'operator' => 'AND' // or you can choose 'IN'
        );
    }
    $new_query = new Wp_Query($args);
    //start loop
    while ($new_query->have_posts() ) : $new_query->the_post(); ?>
    

    hope it helps.