Passing form inputs into multi-taxonomy query

I’m in the process of building out a search page to perform a query on a custom post type and its associated meta and taxonomy. Eventually, I would it to query multiple taxonomies as well as compare values to those in the meta (ie – find posts with foo between X and Y).

I have a pretty good understanding of the query function, thanks to the posts by Otto and Scribu. The problem is I’m struggling to figure out how to pass the variables in via a form. My first attempt was to try the method recommended in this post, but I’m not returning any results (address bar shows: http://sitename.com/?brand=22. Here’s my first attempt (“brand” is one of my taxonomy names):

Read More
<form action="<?php echo home_url('/'); ?>" method="get">
    <p><?php wp_dropdown_categories('taxonomy=brand&name=brand'); ?></p>
    <p><input type="submit" value="Search!" /></p>
</form>

Will a simple HTML form such as this suffice? Or should I be building a function in my functions.php file?

My second attempt was to incorporate a function:

<?php
function get_terms_dropdown($taxonomies, $args){
    $myterms = get_terms($taxonomies, $args);
    $output ='<select multiple="yes" size="3" name="'.$taxonomies.'">';
    foreach($myterms as $term){
        $root_url = get_bloginfo('url');
        $term_taxonomy=$term->taxonomy;
        $term_slug=$term->slug;
        $term_name =$term->name;
        #$link = $root_url.'/'.$term_taxonomy.'/'.$term_slug;
        #$link = $term_taxonomy;
        $output .="<option value='".$term_name."'>".$term_name."</option>";
    }
    $output .="</select>";
return $output;
}
$args = array('orderby'=>'count','hide_empty'=>true);
?>


<form role="search" method="get" id="searchform" action="<?php bloginfo('home'); ?>">
<?php echo get_terms_dropdown(array('brand'), $args); ?>
<input type="submit" id="searchsubmit" value="Search" />
</form>

But again, I’m not able to submit the form/generate results.

Am I on the right track here? Any suggestions are appreciated.

Related posts

Leave a Reply

2 comments

  1. You mention that with your first attempt you get the URL http://sitename.com/?brand=22 which is correct as you’ve used a get method, but you don’t say what code you’re using for the query.

    Personally I use this for my form and would agree with wdalhaj to use post instead of get

    <form id="brandsearch" method="post" action="">
      <select name="brand" id="brand">
        <option value="">All</option>
    <?php
    $theterms = get_terms('brand', 'orderby=name');
    foreach ($theterms AS $term) :
        echo "<option value='".$term->slug."'".($_POST['brand'] == $term->slug ? ' selected="selected"' : '').">".$term->name."</option>n";
    endforeach;
    ?>
      </select>
    </form>
    

    and then for building the query

    $myquery['post_type'] = 'custom-post-name';
    $myquery['posts_per_page'] = -1; // only used if you want all results on one page
    $myquery['tax_query'] = array();
    
    if (!empty($_POST['brand'])) :
        $brand = $_POST['brand'];
        $myquery['tax_query'][] = array(
            'taxonomy' => 'brand',
            'terms' => array($brand),
            'field' => 'slug',
        );
    endif;
    
    query_posts($myquery);
    

    You can put the form into a standard page using a page template or a shortcode (see http://codex.wordpress.org/Function_Reference/add_shortcode) but you’ll be best off creating a page template for your search query and results.

  2. This is kinda tricky solution, but it works:

    1. First you have to make a page template then create a page that uses that template and give it any name for example “advanced_search”.(you can exclude it from being showed if you want).

    2. Make the form action to this page (advanced search).

    3. Now pass the data via the regular post method and receive them in the page template you’v made before via the super global variable $_POST[] .

    4. Now you can make a the query using the variables you’v got via post method.

    Here is more tips:

    -If it didn’t work,you can always interact with WordPress database safely and effectively using the $wpdb object.

    -Debug the query string by echoing it using : <?php echo $query_string; ?>

    its always about how to work around WordPress.