Title: Category smart search function?
Last modified: August 21, 2016

---

# Category smart search function?

 *  Resolved [allcore](https://wordpress.org/support/users/allcore/)
 * (@allcore)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/category-smart-search-function/)
 * Hi,
    I am wondering if your plugin can search specific categories only. If my
   user is in a category and they do a search for a product I only want products
   from that category to come up, not ones from a different category. Is this possible?
 * [https://wordpress.org/plugins/relevanssi/](https://wordpress.org/plugins/relevanssi/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/category-smart-search-function/#post-4598441)
 * Yes, and quite easy. You just need to edit the search form so that it includes
   this:
 * `<input type="hidden" name="cats" value="XX" />`
 * where XX is the ID of the current category. Then the searches will only include
   results from the current category.
 *  Thread Starter [allcore](https://wordpress.org/support/users/allcore/)
 * (@allcore)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/category-smart-search-function/#post-4598518)
 * Thanks, this is good to hear. I love your plugin! I am not a developer so could
   you tell me what file I would be adding that bit of code to? Also just to clarify,
   would the XX be grabbing the category ID dynamically? I am not understanding 
   how each category ID is grabbed; if I have 10 categories each one needs to be
   able to search the products only within each separate category. (Sorry I hope
   I am explaining it clearly)Thanks again for your help.
 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/category-smart-search-function/#post-4598587)
 * No, you need to replace the XX with the category ID yourself. For example, on
   single post pages you can use [get_the_category()](http://codex.wordpress.org/Function_Reference/get_the_category)
   to fetch the categories for the current post. If you always have posts in just
   one category, this works:
 *     ```
       global $post;
       $cat = get_the_category($post->ID);
       $cat_id = $cat[0]->category_ID;
       ```
   
 * Now $cat_id has the ID of the category of the current post.
 * As for where to put this code… it goes inside the search form, and where that
   is, depends on your theme and way you’ve set it up. If you are using the search
   form widget, you need to get a plugin that offers widgets that can execute PHP
   code, and create a new search form in there. If the search form comes from a 
   template file, then you need to edit that template file.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Category smart search function?’ is closed to new replies.

 * ![](https://ps.w.org/relevanssi/assets/icon-256x256.png?rev=3529670)
 * [Relevanssi - A Better Search](https://wordpress.org/plugins/relevanssi/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/relevanssi/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/relevanssi/)
 * [Active Topics](https://wordpress.org/support/plugin/relevanssi/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/relevanssi/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/relevanssi/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * Last activity: [12 years, 3 months ago](https://wordpress.org/support/topic/category-smart-search-function/#post-4598587)
 * Status: resolved