How to Limit Search Results to Specific Post Types in WordPress

Home » Snippets » How to Limit Search Results to Specific Post Types in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking to streamline the search function on your WordPress site? By default, WordPress search covers all of your content – from blog posts to pages to custom post types. If your site has a lot of varying content, this broad-spectrum approach might be overwhelming for your users. In this article, we will teach you how to limit search results to specific post types, offering a more curated user experience for your website visitors.

					function wpturbo_search_filter($query) {
    if ($query->is_search) {
        $query->set('post_type', array('post', 'page'));
    }
    return $query;
}
add_filter('pre_get_posts', 'wpturbo_search_filter');
				

In order to understand how this snippet works, we start by taking a look at the function we have defined: wpturbo_search_filter($query). This function accepts a parameter $query which is an object of the WordPress built-in class WP_Query.

The WP_Query object contains information about the current query and allows developers to change certain properties or variables before the query is executed. This way, we can alter the search behavior.

Within this function, we’re using a conditional statement to check if the current query is a search query. This is achieved by using the is_search method inside WP_Query object:

if ($query->is_search) {
    // code goes here
}

If the above condition evaluates to true, which means the query is indeed a search one, the function will call another method set() from the WP_Query class. The method takes two arguments. The first one is the name of the property we want to set and the second one is the new value:

$query->set('post_type', array('post', 'page'));

In this case, we’re changing the ‘post_type’ property to an array containing two elements: ‘post’ and ‘page’. As a result, this search will only look for results within the ‘post’ and ‘page’ post types on the site, ignoring any other possible post types, like ‘attachment’, ‘revision’, etc.

Finally, we return the altered query with the updated ‘post_type’ property:

return $query;

The last line of the snippet is using WordPress’s add_filter() function to hook the newly made function to a WordPress action hook, ‘pre_get_posts’:

add_filter('pre_get_posts', 'wpturbo_search_filter');

The ‘pre_get_posts’ action hook is triggered right before WordPress executes the database query to fetch posts. By hooking our function to this action, we ensure that our changes to ‘post_type’ property are made just before the query is executed. Consequently, the search results will be filtered to show only posts and pages, as per the tweaked ‘post_type’ attribute.

Always remember to add and adapt the snippet according to your needs, for instance, if you want to include other post types as well, just add them to the array in the set() method call.

Register an account to save your snippets or go Pro to get more features.