Limit Your WordPress Search Results
We all know the default WordPress search function isn’t very great or at least not as good as Google :) , but there are a few tricks out there to help improve the search results. This post will show you different methods of limiting your search results to specific posts types or categories and how to change the default number or search results per page.
Limit WordPress Search Results By Category
Here are a few methods you can use for limiting your search results by category which can be very useful for sites like WPExplorer.com which has sections such as themes, plugins and the blog.
1. Using A Hidden Input Field In Your Searchform.php
This is the method I currently use on WPExplorer.com so when people search the main site they will only get search results from the “WordPress Themes” category. All you need to do is add a hidden input to your searchform.php with an id of “cat” and the category id for the value.
A basic searchform.php
<form method="get" action="<?php bloginfo('home'); ?>/">
<input type="text" size="16" name="s" value="Search" />
<input type="submit" value="Go" />
</form>
Add the following to limit to the category with an ID of 5
<input type="hidden" name="cat" id="cat" value="5" />
Full Code
<form method="get" action="<?php bloginfo('home'); ?>/">
<input type="hidden" name="cat" id="cat" value="5" />
<input type="text" size="16" name="s" value="Search" />
<input type="submit" value="Go" />
</form>
2. Adding A Query Statement To Your Search.php File
Another useful method for limiting your search results to specific categories is to add a query to your search.php file right before the if statement.
Including Categories To The Search
By using positive ID’s in the query you can define which categories you want to show in your search results (show categories with ID’s 1,2 and 3).
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; query_posts("s=$s&paged=$paged&cat=1,2,3"); ?>
Excluding Categories From The Search
Alternatively you can use negative ID’s to exclude certain categories from your search page (exclude the category with an ID of 7)
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("s=$s&paged=$paged&cat=-7");
?>
Exclude Pages From Your Search Page
Below are a couple ways you can limit your search results to exclude pages from the results and show only posts.
Using a Function
Simply insert the following function to your functions.php file
//Remove pages from search results
function exclude_pages_from_search($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts','exclude_pages_from_search');
Using A Conditional In Your Search.php File
Another way to remove pages from your search.php file is to insert a conditional in your search.php file right after “while ( have_posts())”.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?> <?php //exclude pages from the loop if (is_search() && ($post->post_type=='page')) continue; ?>
Limit Search To Post Type
One of the reasons to use Port Types in WordPress is because you don’t want these in your feed or main wp loop, that is why they are perfect for things like sliders, testimonials, services…etc. However, your custom posts may still appear in your search results page.
1. Exclude A Custom Post Type From Search Results
All you have to do to exclude custom posts from search results is to set the following argument when defining your custom post (more info):
'exclude_from_search' => true
2. Using A Hidden Field In The Search Form To Show Only Posts From A Specific Custom Post Type
Alternatively you can use a hidden field in your search form the same way you did for limiting categories if you want to set up an advanced search form which will only search through the specified custom post type.
<input type="hidden"name="post_type"value="coupons"/>
This method is great if you have a certain category on your site that uses custom post types and you want users to be able to search it. For example it would be great to have a search form on a “coupons” page (like shown above) so users can search just the coupons section of your site.
Changing The Number Of Results Per Page
By default WordPress uses the number defined under Settings –> Reading (blog pages show at most…) to define how many results appear on the search results page. If you would like to show more, less or infinite results on your search page you can do so using the following code.
<?php$posts=query_posts($query_string.'&posts_per_page=10'); ?>
Make sure to insert it in your search.php file right before the if statement. Example:
<?php$posts=query_posts($query_string.'&posts_per_page=-1'); ?> <?phpif(have_posts()) : ?> <?phpwhile(have_posts()) : the_post(); ?>
This will set your search results to “10″ per page. You can change the number to whatever you want. For making your search results unlimited use -1.



