Menu Close

How to Limit Your WordPress Search Results

default

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.

How to 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 role="search" method="get" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
	<label class="searchform-label">
		<span class="screen-reader-text"><?php esc_html_e( 'Search', 'text_domain' ); ?></span>
		<input type="search" class="searchform-input" name="s" placeholder="<?php esc_attr_e( 'Search', 'text_domain' ); ?>">
	</label>
	<button type="submit" class="searchform-submit"><?php esc_html_e( 'Submit', 'text_domain' ); ?></button>
</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 role="search" method="get" class="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>">
	<label class="searchform-label">
		<span class="screen-reader-text"><?php esc_html_e( 'Search', 'text_domain' ); ?></span>
		<input type="search" class="searchform-input" name="s" placeholder="<?php esc_attr_e( 'Search', 'text_domain' ); ?>">
	</label>
	<input type="hidden" name="cat" id="cat" value="5">
	<button type="submit" class="searchform-submit"><?php esc_html_e( 'Submit', 'text_domain' ); ?></button>
</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' ) ?: 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' ) ?: 1;
query_posts( "s=$s&paged=$paged&cat=-7" );
?>

How to Exclude Pages From Your Search Results?

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:

/**
 * Set search results post types.
 */
function wpexplorer_exclude_pages_from_search( $query ) {
	if ( $query->is_search() && $query->is_main_query() && ! is_admin() ) {
		$post_types = [ 'post' ];
		$query->set( 'post_type', $post_types );
	}
}
add_filter( 'pre_get_posts','wpexplorer_exclude_pages_from_search' );

Make sure to modify the function to change the $post_types variable to include all the post types you want to display in your search results.

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())”. This isn’t recommended if you are using a 3rd party theme because if you update the theme in the future you will lose your edits, if it’s your own custom theme then it’s fine.

if ( have_posts() ) : while ( have_posts() ) : the_post();
   // Exclude pages from the loop.
   if ( 'page' === get_post_type() ) {
       continue;
   }

Exclude a specific page or pages

You can also exclude specific posts/pages from your search results pages on their ID’s by using the following code added in the functions.php file. Simply alter the array of ID’s to include the ID’s of the pages or posts you wish to exclude.

/**
 * Exclude pages from search results by ID.
 */
function wpexplorer_exclude_pages_from_search( $query) {
	if (  ! is_admin() && $query->is_search() && $query->is_main_query() ) {
		$exclude_ids = [ 7, 19 , 21 ]; // Array of the ID's to exclude.
		$query->set( 'post__not_in', $exclude_ids );
	}
}
add_filter( 'pre_get_posts', 'wpexplorer_exclude_pages_from_search' );

How to Limit Search Results by 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 via the wp_register_post_type function.

'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="portfolio">

This extra field would go inside your search form and you would simply replace where it says “portfolio” with your own custom post type name. This method is great if you have a certain section on your site that uses custom post types and you want users to be able to search through those specific posts only.

How to Change the Number of Search Results?

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:

/**
 * Alter search posts per page.
 */
function wpexplorer_search_posts_per_page( $query) {
    if ( ! is_admin() && $query->is_search() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', '10' );
    }
}
add_filter( 'pre_get_posts', 'wpexplorer_search_posts_per_page' );

This code would go in your functions.php file. Preferably in your child theme if working with a theme from another developer. The code 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. Or utilize a plugin like Toolset Search to add pagination to your results.

WordPress by default searches based on any content inside your posts and pages. It would be possible to alter the way the search works via functions but it’s fairly complex, so if you want to alter the way the search actually works (what it searches for) you may consider instead using a plugin.

WP Extended Search

Once such option is the WP Extended Search plugin. This plugin allows you better control the default search if you want to search by meta, categories, tags, terms, title…etc. You can include or exclude certain post types from search, exclude posts that are older than a specific date, alter the number of items displayed on the search results, customize the ordering and more!

Ivory Search

Another search plugin you may want to consider is Ivory Search. This plugin allows for custom search forms with options to configure each form separately. So you can create a custom search for a specific post type like your portfolio, or create a search for you homepage that searches everything on your site excluding your staff. There are options to search based on posts, pages, products, taxonomies, custom fields, author and more.

Relevanssi

Relevanssi Better Search Plugin

And the last plugin we’d like to share is Relevanssi. By far the most advanced option, Relevanassi offers many features to improve your WordPress site search function including settings to limit search results. You can easily restrict searches using the plugin’s indexing options. Simply choose what post types and taxonomies you’d like to index and/or exclude from your search, select category restrictions/exclusions, add specific post id exclusions (like your checkout page perhaps) and much more. There is also a premium version of this plugin if you’re looking for even more features as well.

View Source
Posted in WordPress