Since when I started wordpress, I had a question in my mind, why wordpress doesn’t give multiple search option? I googled a lot, but couldn’t find a plugin or code which exactly works.
So finally decided to go more into deep of wordpress and php, and came up with a code which works.
Functioning of this code: Searches all posts using a custom search box where 2 drop-down box will be displayed with values of categories in it.
Have you ever played with search URL? Or tags URL or Categories URL?
Very few might have knowledge that wordpress supports multiple tags or multiple categories. Don’t believe me, in your URL, type this
http://yourblog.com/tag/tag1+tag2
Or
http://yourblog.com/tag/tag1,tag2
Now what does this means? Simple when you type tag1+tag2 it will show you posts with having both the tags. And when you type tag1, tag2 it will show you all the posts having either tag in it.
Same works for categories.
http://yourblog.com/category/cat1+cat2
http://yourblog.com/category/cat1,cat2
Now I tried this same functionality in search URL of Wordpress.
http://yourblog.com/?s=html&cat=114
And woila it worked!!!!
Here “s” is your search term and “cat=114” is your category ID. This will work and display all the posts with the search term only in category ID 114.
But the only problem in this is that it will just take one category at a time.
Wordpress doesn’t supports this URL
http://yourblog.com/?s=html&cat=114+115
Here comes a big problem. After a lot of search I found that “+” is considered as a space in URL. So we need to write a function which will make this space act as a + operator.
[php]
add_action( 'parse_request', 'category_search_logic', 11 );
function category_search_logic( $query ) {
if ( ! isset( $query->query_vars[ 'cat' ] ) )
return $query;
// split cat query on a space to get IDs separated by '+' in URL
$cats = explode( ' ', $query->query_vars[ 'cat' ] );
if ( count( $cats ) > 1 ) {
unset( $query->query_vars[ 'cat' ] );
$query->query_vars[ 'category__and' ] = $cats;
}
return $query;
}
function check()
{
$url = $_SERVER["REQUEST_URI"];
$query = str_replace('&cats=', '+', $url);
if(isset($query) && ($query != $url))
{
header('Location:'.$query);
}
}
add_action('parse_query', 'check');
[/php]
After getting this function in our functions.php file, the multiple categories URL with search will work.
So it’s time to design a simple search form.
Paste this anywhere in your theme.
[html]
<!-- Search Form -->
<div style="margin:10px 0 10px 0;">
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
<table cellspacing="5">
<tr>
<td>
<label for="s" style="margin:10px;">Search</label>
</td>
<td>
<input type="text" class="field" name="s" style="width: 100px;"/>
</td>
<td>
<?php wp_dropdown_categories('show_count=1&hierarchical=1'); ?>
</td>
<td>
<?php wp_dropdown_categories('show_count=1&hierarchical=1&name=cats'); ?>
</td>
<td>
<input type="submit" class="submit" name="submit" id="searchsubmit" value="Search" style="display: block; position: relative; top: 7px;"/>
</td>
</tr>
</table>
</form>
</div>
<!-- Search Form -->
[/html]
That’s it, now just go and try your wordpress site will be having a multiple category search option. No plugin and no hundred lines of code, and best part, it’s fully flexible to use. If you want one more category drop down then just copy paste the category php code one more time.