Today I had to find a solution for searching custom post within custom taxonomy and displaying results in custom search template.
First of all I’ve added simple search form to category with custom posts, copied from searchform.php:
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>"> <input type="text" name="s" id="s" placeholder="Nazwisko..." /> <input type="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search'); ?>" /> </form>
Than I had to make search to look only in one taxonomy:
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>"> <input type="text" name="s" id="s" placeholder="Nazwisko..." /> <input type="hidden" name='nazwisko' value='lista-nazwisk'> <input type="submit" name="submit" id="searchsubmit" value="<?php esc_attr_e( 'Search'); ?>" /> </form>
where nazwisko is the name of my custom taxonomy and lista-nazwisk is a name of category created in taxonomy. Lista-nazwisk contains subcategories, and search engine will also look form posts within them.
Code above allows us to search just in custom taxonomy, but how to make separate search results template?
In my template I had a custom template for displaying custom posts – the name of template is taxonomy-nazwisko.php where “nazwisko” is the name of taxonomy. I copied this file and renamed it to search-custom.php then in search.php in the beginning I’ve added simple condition check:
if($_GET['nazwisko']){
require_once('taxonomy-nazwisko.php');
}
else{
HERE GOES ORIGINAL search.php content
}
the condition checks if the name of taxonomy (“nazwisko”) is passed in url by search query, if so it displays search template, in other cases it will display standard search results template.


