Posted on

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.

27 comments Filtering results by category

  1. 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,

  2. 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.

  3. 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.

  4. 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?

    1. 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.

  5. 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?

  6. 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?

    1. 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.

  7. 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.

  8. Hi Relevanssi, great plugin!

    After reading your Knowledge Base, we have had success creating Filters By Category, Author and Custom Post Type on our client’s Resource Library – check it out: http://smartgridcc.staging.wpengine.com/?ResL=true&s=best

    I am now trying to display the number of search result WITHIN each filter parameter. So, for instance, I want to know that the category ‘Best Practices’ contain 24 results before I click on it by displaying on my sidebar:

    Best Practices (24)

    Do you have an ingenious solution? Again many thanks, great plugin.

    UPDATE: This site has the functionality we are looking for: http://www.apiahf.org/search/apachesolr_search/?filters=tid:46

  9. When filtering by category, would it be possible to make it so a filter’s link doesn’t go anywhere unless the search results actually have any posts in that category, so you don’t get a 404 if you click a category with no results?

  10. Is it possible to search/filter within two category? For example if I have catergory CAR and then category CITY. And user first selects from car cateogory and then city and then hit search button and then it returns all posts that are in those two categories. But also that, user selects just from car category and leaves city category empty -> search would just search with in one category.

Leave a Reply to Adrian Dennis Cancel reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *