Have different search results template depending of custom post type searched

So I have found the solution, I leave it here if anyone needs something like this in the future.

On your search form add a hidden field:

<input type="hidden" name="post_type" value="post_type_name" />

Then on your search.php file add the following WITHIN the loop:

    <?php
    if(isset($_GET['post_type'])) {
        $type = $_GET['post_type'];
        if($type == 'post_type_name_1') {?>
        <!--Your Code for this post_type-->         
        <?php    
        } elseif($type == 'post_type_name_2') {?>
             <!--Your Code for this post_type-->
        <?php }
    }
    ?>

Best!

Leave a Comment