How to Limit Your WordPress Search Results

作者: zengde 分类: wordpress 发布时间: 2023-05-30 02:47

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 method="get" action="">
    <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="">
    <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" );
?>

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

// Remove pages from search results
function exclude_pages_from_search( $query ) {
    if ( $query->is_search() && $query->is_main_query() && ! is_admin() ) {
        $query->set( 'post_type', 'post' );
    }
}
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())”. 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.

<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
   // Exclude pages from the loop.
   if ( is_search() && ( $post->post_type=='page' ) ) {
       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 specific posts/pages from search
function myprefix_exclude_pages_from_search( $query) {
	if ( $query->is_search() && $query->is_main_query() && ! is_admin() ) {
		$exclude_ids = array( 7, 19 , 21 ); // Array of the ID's to exclude
		$query->set( 'post__not_in', $exclude_ids );
	}
}
add_filter( 'pre_get_posts', 'myprefix_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 (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="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 myprefix_search_posts_per_page( $query) {
    if ( $query->is_search() && $query->is_main_query() && ! is_admin() ) {
        $query->set( 'posts_per_page', '10' );
    }
}
add_filter( 'pre_get_posts', 'myprefix_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.

Recommended Plugins to Improve WordPress Search 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

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

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.

reference: https://www.wpexplorer.com/limit-wordpress-search/