How to Filter Post Types from Search Results in WordPress

Home » Snippets » How to Filter Post Types from Search Results 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

Have you ever wanted to fine-tune the search results on your WordPress website, specifically by excluding certain post types? Maybe you have custom post types that you don’t want to appear in the search results, or you want to prioritize certain post types over others. In this article, we will explore how to easily filter post types from search results in WordPress, giving you more control over the content that is displayed to your users.

					function wpturbo_filter_post_types_from_search_results($query) {
    if (is_admin() || !$query->is_main_query()) {
        return;
    }
    
    if ($query->is_search) {
        $post_types_to_exclude = array('page', 'attachment');
        
        $query->set('post_type', array_diff($query->get('post_type'), $post_types_to_exclude));
    }
}
add_action('pre_get_posts', 'wpturbo_filter_post_types_from_search_results');
				

The code snippet above is used to filter specific post types from the search results in WordPress. By adding this code to your theme’s functions.php file or a custom plugin, you can control which post types are displayed when performing a search on your WordPress site.

Let’s go through the code step by step to understand how it works.

First, we define a new function called wpturbo_filter_post_types_from_search_results(). This function takes in the $query parameter, which represents the current query being executed.

Next, we perform a couple of checks. The is_admin() function checks if the current page is the admin dashboard, and the !$query->is_main_query() checks if the query being executed is the main query. If either of these conditions is true, we return immediately, as we only want to modify the search results on the frontend.

After that, we check if the current query is a search query by using the $query->is_search property. If it is, we proceed to filter the post types.

In this example, we want to exclude the ‘page’ and ‘attachment’ post types from the search results. We create an array called $post_types_to_exclude and populate it with the post types we want to exclude.

To modify the post types used in the search query, we use the $query->set() method. This method allows us to change the value of a query parameter.

We access the existing post types in the query using $query->get('post_type'), which returns an array of post types. We then use the array_diff() function to remove any post types that are present in both the existing array and the $post_types_to_exclude array. This effectively filters out the post types we want to exclude.

Finally, we hook our wpturbo_filter_post_types_from_search_results() function to the pre_get_posts action using add_action(). This ensures that our function is called before the search results are fetched, allowing us to modify the query parameters.

By adding this code to your WordPress site, you can easily exclude specific post types from being included in the search results, providing a more refined and tailored search experience for your users.

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