How to limit search results to Custom Post Types in WordPress
We are developing a new branded blog/portfolio theme UltraDev and there is a need to limit the search result to our custom post type (i.e.portfolio).

Using the default WordPress search just wasn’t cutting it. Using Google Search also was not a viable option. Therefore, we decided to modify the default search form a bit by adding only one line.
[php]
<input type="hidden" name="post_type" value="post_type_name" />
[/php]
Here you should leave “post_type” as it is as we want to limit the search result to a custom post type. And you have to replace “post_type_name” with your own custom post_type name.
For example, the structure of the default search form is like:
[php]
<form role="search" method="get" id="searchform" action="<?php echo home_url( ‘/’ ); ?>">
<div><label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>
[/php]
we want to limit the search result to ‘portfolio’ post type, then the code should be:
[php]
<form role="search" method="get" id="searchform" action="<?php echo home_url( ‘/’ ); ?>">
<div><label class="screen-reader-text" for="s">Search for:</label>
<input type="text" value="" name="s" id="s" />
<input type="submit" id="searchsubmit" value="Search" />
<input type="hidden" name="post_type" value="portfolio" />
</div>
</form>
[/php]
Line 5 is the line we newly added.
Put it further, how to limit the search result to several post types instead of only one as above?
You’ll find how easy it is as well. Just use following codes and add to your search form:
[php]
<input type="hidden" name="post_type[]" value="post_type_name_1" />
<input type="hidden" name="post_type[]" value="post_type_name_2" />
<input type="hidden" name="post_type[]" value="post_type_name_3" />
…
[/php]
Don’t forget to replace the value with your custom post types.


















