How to Exclude a Custom Post Type from WordPress Search Results

Home » Snippets » How to Exclude a Custom Post Type from WordPress Search Results
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 using custom post types on your WordPress site and want to exclude them from search results? By default, WordPress includes all post types in the search query, including any custom post types you have created. However, there might be instances where you want to exclude specific post types from appearing in search results. In this article, we will guide you through the process of excluding a custom post type from the WordPress search functionality, helping you to fine-tune your search results and provide a better experience for your users.

					function wpturbo_exclude_custom_post_type_from_search($query) {
    if (!is_admin() && $query->is_main_query()) {
        $post_type = $query->get('post_type');
        if ($post_type && $post_type === 'your_custom_post_type') {
            $query->set('post_type', array('post')); // replace 'post' with the post type you want to include in the search
        }
    }
}
add_action('pre_get_posts', 'wpturbo_exclude_custom_post_type_from_search');
				

To exclude a custom post type from the search results in WordPress, you can use the code snippet shown above.

The code snippet defines a function called wpturbo_exclude_custom_post_type_from_search(). This function will be responsible for modifying the main query to exclude the specified custom post type from the search results.

The first condition in the code snippet checks if the current request is not in the admin area and if it is the main query. This ensures that we are only modifying the main query on the front-end of the website, excluding the custom post type only from the search results.

Next, we retrieve the current post type from the query object using the get('post_type') method. We store the post type in the variable $post_type.

Then, we check if the $post_type exists and if it matches the custom post type that we want to exclude from the search. In this example, the custom post type is your_custom_post_type. If the condition is true, we modify the query to include only the default post type post in the search results. You can replace 'post' with the desired post type if you want to include a different post type in the search.

To modify the query, we use the set('post_type', array('post')) method on the query object. This sets the post type to an array containing 'post', which is the default post type. By doing this, we exclude the custom post type from the search results.

Finally, we hook the wpturbo_exclude_custom_post_type_from_search() function into the pre_get_posts action. This action is triggered before the main query is executed, allowing us to modify the query parameters. By adding our function to this action, we ensure that our modifications are applied correctly.

By adding this code snippet to your WordPress site, you can easily exclude a custom post type from the search results, providing a more focused and relevant search experience for your users.

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