Filtering results by category

In order to filter search results by category, use the following code:

$url = get_bloginfo('url');
$url = esc_attr(add_query_arg(array('cat' => '10', 's' => get_search_query()), $url));
echo "<a href='$url' rel='nofollow'>Filter by category 10</a>";

Put this in your search results template. What the code does is to create a link that takes the current search query and adds the category variable to it. This link filters the results by category 10, change the number to correct category ID to make it filter by the category you want to.

Remember that whenever you link to search results like that, it’s a good idea to tell Google and other bots to avoid indexing the results. The link above has the “rel=’nofollow’” instruction, but you can also use robots.txt to prevent bots from spidering your search results pages.

  • Adrian Dennis

    Ok, Thanks, It Works, But Now Comes The Hard Part,

    Of Styling It, So, It Has A Drop Down, Where They Select The Category

    To Filter Their Search, From,

  • Adrian Dennis

    Sorry, Found A Plugin, That Allows Search In Category,

    http://wordpress.org/extend/plugins/search-by-category/

    i posted the link above, should you try and incorporate in a future update

    as that plugin, does the filtering better, not trying to be rude

    also you can delete my previous comment

  • http://openeye.net Zach Shallbetter

    I would like to know this as well. It would be awesome if they were able to select a category from a drop-down for filtering.

  • J. Kerchner

    I’d really like to see this as an included feature, too. Relevanssi is all about making a better search experience for the user, and being able to have a drop down list of categories would certainly help.

    Would be great if this were incorporated into a widget so I could just drop a search widget it into a sidebar and be able to configure which categories I want displayed in the drop down list.

    • http://www.mikkosaari.fi/ Mikko Saari

      We’ve started working on Relevanssi 3.0 (and Premium 2.0) – a search widget would be a nice addition. I’ll put it on the to-do list.

  • http://www.ronrichardsonwebdesign.com Ron Richardson

    I’d like to have radio buttons to choose multiple categories. Wondering if your current plug-in could be modified for this?

    • http://www.mikkosaari.fi/ Mikko Saari

      Go ahead and use radio buttons, no problems with that. Relevanssi doesn’t care what kind of user interface element is used.

  • Kathy Yang

    I was wondering if there was a way to filter results by author. Could I tweak the filter category code array so it queries author? I’ve been playing around with that idea but haven’t been successful as of yet.

    • http://www.mikkosaari.fi/ Mikko Saari

      Yes, it is possible. If you need help, send me email to mikko at mikkosaari.fi with your code, and I’ll take a look at it.

  • Peter

    Is there a way to share this ‘author filter’ code/form in the knowledge base?
    Thank you!

    • http://www.mikkosaari.fi/ Mikko Saari

      It is very simple: just replace “cat” with “author” in the example and the category ID number with the author ID number.

  • http://rebelliousaddictiontwo.com heather samee

    Either I have done something wrong, or this just isn’t working for me. The program is not seeing dresses and dress as the same even though I put them in the Synonyms area. Nor is it recognizing the other products in their. My site is WooCommerce. I don’t have a search results template as far as I can tell. I don’t know what to do. Should I remove the program?

    • http://www.mikkosaari.fi/ Mikko Saari

      If I remember correctly, Relevanssi doesn’t work out of the box with WooCommerce (or Woo themes in general). So yeah, probably better to look for other search solutions.

  • http://www.thefamousartists.com Michele

    I’m working on giving my site a total makeover. It would include using Relevanssi.

    I was wondering if it would be possible to somehow include the category descriptions in the search and the category pages as part of the returned results. On my site, I’m using category pages for biographies of artists and then the posts are the works of the artist. So, ideally, a search for a name would return the artist’s category/bio, other bios that mention them and any posts that mentions them.

    Is that possible? Would it be something you might consider doing in a later version?

    • http://www.mikkosaari.fi/ Mikko Saari

      That is already available in the Premium version.

  • DavidM

    I like it. But it is complex to maintain if you want all your categories in the filter. Maybe there is a way to autogenerate all the filter links for all categories?

    • http://www.mikkosaari.fi/ Mikko Saari

      Yes, and it’s not hard at all. Just fetch a list of the categories (use wp_list_categories() and modify the list it outputs, or fetch them from the database yourself) and apply the code in this post for each of them. Basic WordPress programming.

  • DavidM

    Thanks, but basic programming is far beyond me… :) I have no idea about how to apply the code to the results of wp_list_categories(), apart from copypasting every cat ID by hand. Sorry if I’m asking too much.

  • http://www.mikkosaari.fi/ Mikko Saari

    Well, it is slightly complicated, so I’d have to think about how to do it, no instant solution available.

    • DavidM

      No need to do that! Thank you anyway, it’s a fundamental plugin for us.