Post Pic

How to: limit search to specific categories

If for some reason, you’d like to exclude some categories from searches, you have to tweak WordPress a bit because there’s no built-in solution to this problem. Happilly, here’s a code to do that job!

To achieve this recipe, replace the categories IDs on line 3 and paste the following code on your search.php template:

<?php if( is_search() )  :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts("s=$s&paged=$paged&cat=1,2,3");
endif; ?>

You're done! See you tomorrow ;)

6 Responses

Jan 22 2009 01:41

Yes – I’d mentioned/asked you about this (via a tweet) in regards to your Google Custom Search.

Some 3rd party applications like PicoSearch allow you to filter/limit search via dropdowns etc. (Serarch within a category etc). ExpressionEngine also uses this type of feature. I haven’t seen it implemented in WordPress though. Your approach hardcodes the searchable cats, but a dropdown list might be nice – allowing search limits on things like cats, tags, authors etc.

Mar 29 2009 13:01

Dude i think it should be

Im no expert but the thing wont work without the minus sign.
Regards,
Ace.

Mar 29 2009 13:02

“”"”
“”"”

Mar 13 2010 11:56

Why not use or in the search form?

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required