Using standard search function with custom post type

Ok, so I did a little more digging around and it turns out to be pretty easy. I just needed to add a hidden input into the search form. Posting this here for anyone else looking for the answer:

<form class="search" action="<?php echo home_url( "https://wordpress.stackexchange.com/" ); ?>">
        <input type="search" name="s" placeholder="Search&hellip;">
        <input type="submit" value="Search">
        <input type="hidden" name="post_type" value="custom-post-type">
</form>

Obviously you will need to replace the value “custom-post-type” with your own custom post type.

Leave a Comment