I'm building a custom search form with keywords and drop down lists (categories, date, and country (have a separate plugin for this)).
In the searchform.php I have the input field and then select>option tags.
<!-- searchform.php (form) -->
<form action="/" method="get">
<input type="text" name="s" id="search" value="<?php the_search_query(); ?>" />
<select name="category">
<select name="country">
<option value=""></option>
<option value="UK">UK</option>
<option value="USA">USA</option>
<option value="Brazil">Brazil</option>
<option value="Poland">Poland</option>
<option value="Canada">Canada</option>
</select>
<select name="year">
<option value=""></option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
</select>
</form>
<?php
// search.php (results)
$args = array(
's' => $_GET['s'],
'country' => $_GET['country'],
'year' => $_GET['year']
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
// Do Stuff
<h3 class="search-title"><?php country_tag($post->ID); ?><a href="#" data-siteurl="<?php bloginfo('url'); ?>" rel="<?php the_ID(); ?>" title="<?php the_title_attribute(); ?>" class="search-post-title"><?php the_title(); ?></a></h3>
<?php
endwhile;
endif;
?>
Problem: Currently if I leave the selects empty it sends them as empty strings in the get url (?s=keyword&country=&year=
). This means that nothing will be found in the search. (I am guessing because its saying 'find matching posts with 'keyword' AND an empty string). So I want ?s=keyword
to be passed if others are unused.
Perhaps I've setup the code wrong, likely as I've just started playing around with WP_Query.