How to Create Multiple Search Templates for Custom Post Types in WordPress

Home » Snippets » How to Create Multiple Search Templates for Custom Post Types in WordPress

Created with:



Creator: WPTurbo Team

Customize with WPTurbo AI

Add Snippet To Project

New Project
Add To Existing Project

Are you struggling to create different search templates for your custom post types in WordPress? By default, WordPress uses the same search template for all post types, which may not be ideal if you want to display search results differently based on the post type. In this article, we will guide you through the process of creating multiple search templates for your custom post types, allowing you to customize the search experience for each post type on your website.

					function wpturbo_custom_search_templates($templates) {
    if (is_search()) {
        $post_types = get_query_var('post_type');
        if (!empty($post_types)) {
            foreach ($post_types as $post_type) {
                $templates[] = 'search-' . $post_type;
    return $templates;
add_filter('template_include', 'wpturbo_custom_search_templates');

In this section, we will explain how to create multiple search templates for custom post types in WordPress using the provided code snippet.

To get started, we define a function named wpturbo_custom_search_templates that will be responsible for modifying the search templates to include templates specific to custom post types.

We begin by checking if the current page is a search page using the is_search() function. If it is, we proceed to retrieve the post types used in the search query using the get_query_var('post_type') function.

Next, we check if there are any post types found in the search query by using the !empty($post_types) condition. If post types are found, we enter a foreach loop to iterate through each post type.

Inside the loop, we append the post type name to the string 'search-' using the concatenation operator (.) and then add it to the $templates array.

By doing this, we are effectively creating a dynamically generated search template for each post type. For example, if the search query includes a custom post type called "product", the template name would be 'search-product.php'.

Finally, we return the $templates array from the function.

To incorporate this functionality into WordPress, we need to hook the wpturbo_custom_search_templates function into the template_include filter using the add_filter function.

This filter allows us to modify the template that WordPress will use to render a page. By adding our function to this filter, we can dynamically add and use custom search templates based on the post types present in the search query.

Once the code snippet is implemented and activated, WordPress will automatically use the appropriate custom search template for each post type when performing a search.

This technique can be especially useful when working with multiple custom post types and wanting to have unique search layouts or designs for each one.

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