Tuesday, June 18, 2013

Including/Excluding a Category From Wordpress Search Query

Recently I was working on a Wordpress site where I had to design two types of search. Basically the searches needed to identify a particular post category. I designed two search forms one for general search (searchform.php) and the other for category specific search (searchform-category.php). Further deatils are as follow:

  1. General Search:
    • The General Search was meant to search the entire site except a particular category.
    • This meant that I needed to exclude that particular category.
    • For excluding the category, I simply added the the following hidden field:
        • where 5 denotes the category ID to exclude.
      • In case you want to exclude multiple categories the following should work:
  2. Category Specific Serach (Advanced Search):
    • The advanced search was meant to search in on specific category and was available to only logged in users.
    • This meant that I needed to specifically instruct WP which category to search in.
    • In my advance/category search form I added the following:
        • where 5 denotes the category ID to search in.
      • In case you want to search in multiple categories, the following will work for you
The support forums on Wordpress.org helped me a lot, but there was no clear cut and tested solution available there so I wrote this post for myself and other like me out there :). The above have been tested with Wordpress 3.5.1.

I will be looking at Wordpress Theme Development and Customization in the coming articles, till then:

Happy search customization on Wordpress. :D

No comments:

Post a Comment