How do I create a form to search a custom post type

Basically how would I create a form to search only within a custom post type?

Related posts

Leave a Reply

1 comment

  1. just add

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

    to your search form and obviously replace post type name with your custom post type name.

    and to make sure that only your custom post type return as results add:

    function mySearchFilter_0987($query) {
        $post_type = $_GET['post_type'];
        if (!$post_type) {
            $post_type = 'any';
        }
        if ($query->is_search) {
            $query->set('post_type', $post_type);
        };
        return $query;
    };
    
    add_filter('pre_get_posts','mySearchFilter_0987'); 
    

    Hope this helps