Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question

1 Answer 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

share|improve this answer
    
not working, nothing displayed –  Jeff Feb 23 '11 at 17:47
    
make sure you have the post_type correctly spelled. I was having the same issue only to remember there was no "s" at the end of my post_type. I also got nothing found when I used the function provided above. Just the <input> revision works fine. –  chris_s Aug 2 '11 at 16:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.