Customize Search Results for Custom Post Type

Is there a way to use a custom PHP template for search results of a custom post type. I know that you can have specific archive and category templates for custom post types. For example, archive-custom.php. But, the same doesn’t work for search.php. Any suggestions?

1 Answer
1

According to this answer of yours, you can do the following inside your search.php:

if (isset($_GET['post_type']))
    get_template_part('search', $_GET['post_type']);
else
    // no post_type given

Then you have to set up the search-{post_type}.php files.

If you want to handle only some of the existing post types differently, do this by means of a switch:

$type = (isset($_GET['post_type'])) ? $_GET['post_type'] : '';
switch ($type) {
    case 'attorney':
    case 'criminal':
    case 'musician':
        get_template_part('search', $type);
        break;
    default:
        // default search stuff
}

Leave a Comment