Custom search box: how do I only search for nodes of a specific content type?
O_o - May 3, 2007 - 11:15
I have made a custom content type which I call "FAQ Page". Nodes of this type have a heading (question), body (additional info about hte question), additional body (answer to the question) as well as some vocabularys. I would like to have a "Search the FAQ" function on my "Help Center" page. This search block should only search for nodes of type "FAQ Page" and ignore all other content types. How can I do that?
Thanks in advance.

I just did this today so
I just did this today so it's freah in my head.
Investigate the source of the form that gives you 'advanced search'
Copy anything that looks useful.
I ended up with:
<form action="/search/node" method="post" id="search-form" class="search-form">
<div class="form-item">
<input type="text" class="input-text" value="" size="25" name="keys" />
<input type="image" value="Search" name="op" title="Search" alt="Search" src="/themes/mytheme/images/search-submit.gif" />
<input type="hidden" value="<?php print drupal_get_token('search_form'); ?>" name="form_token" />
<input type="hidden" value="search_form" id="edit-search-form" name="form_id" />
<input type="hidden" name="type[advertiser]" id="edit-type-advertiser" value="advertiser" />
</div>
</form>
This interesting bits are:
<input type="hidden" name="type[advertiser]" id="edit-type-advertiser" value="advertiser" />where advertiser was my node-type
and
<input type="hidden" value="<?php print drupal_get_token('search_form'); ?>" name="form_token" />... which I learnt today.
I trimmed a little bit of formatting junk out to make this paste simpler, but this is what you need.
.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/
Thanks!
Thanks dman! It works like a charm. You really should put that into the Handbook for others to read. So simple yet so useful. :)
For others interested in this. Use dman's code above with his instructions and add it to a block with input format set to PHP. Then you'll have a block which you can use in views, panels etc.
Very cool, but...
I have a problem with this. I get:
"Validation error, please try again. If this error persists, please contact the site administrator."
Weird. I understand that it's most likely the form token, but I did as you guys said. :-\
Nevermind...
I somehow deleted the 'name="form_token"' from it which broke things.
Works great now, thanks!!!!
What about multiple content types?
How would I edit this to handle multiple types like say:
page, blog, and inventory_item types
but not
job_request, site_response, or client_info types
P.S.
Figured out you could just add another hidden field per content type. However is there a way to turn this into a select list?
Not tested, but something
Not tested, but something like
<select name="type[]"><option value="advertiser" >advertiser</option>
<option value="faq" >faq</option>
<option value="blog" >blog</option>
</select>
May do it.
This code is a little dated now (although it still works, there may be better ways)
See also
http://drupal.org/project/search_type
http://drupal.org/project/custom_search_box
http://drupal.org/project/taxonomySearch
and others. These look like D5 mainly :-/
.dan.
Didn't work I'm afraid :/ and
Didn't work I'm afraid :/ and none of those modules achieve what I'm trying to do.
Is there a way you could do this with views? I'm trying to make a custom-designed search block.
Yes!
This works great. Just what I was looking for. Thanks!