Support » Plugin: Relevanssi - A Better Search » Category smart search function?

  • Resolved allcore

    (@allcore)


    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/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Mikko Saari

    (@msaari)

    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.

    allcore

    (@allcore)

    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

    (@msaari)

    No, you need to replace the XX with the category ID yourself. For example, on single post pages you can use 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.