Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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):

<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.

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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.

share|improve this answer
    
Thanks for the info, @Sarah. Does the query just go in its own PHP file (that's how I interpreted @wdalhaj's answer)? IE - should the form action point to "brandSearch.php," which only contains $myquery['post_type'] = 'custom-post-name';... That's what I'm trying now at least, but getting "Search Results for gearSearch.php" and a url of http://sitename.com/search/gearSearch.php. –  nfaust Oct 2 '11 at 13:22
    
The query goes into a PHP file into your template directory as a new page template, and then you create a page and assign it that template as per wdalhaj's answer. You don't have it as a .php page, you keep it contained within WordPress else it won't work (not without including the WP blog header anyway) You could have the search on the same page, so the order in the code would be form > search > query results or you could have the form anywhere on the site but point to the search page with the search query and results on. Your form action would then be something like /search-results/ –  Sarah Oct 2 '11 at 13:46
    
Thanks so much @Sarah - that was a huge help. Just got it going now. You were correct, I was sending it to its own .php file outside of WP. Moving it over to a page template worked. Thank you again. –  nfaust Oct 2 '11 at 13:57

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.

share|improve this answer
1  
Thanks for your help @wdalhaj - I was having trouble getting my head around the page template requirement. –  nfaust Oct 2 '11 at 13:56
    
Please use proper lower-/uppercase letters. At least this isn't chat... –  kaiser Mar 11 '13 at 2:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.