Skip to main contentSkip to footer

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 "Filter by category 10";

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.

Your account

Not logged in. Log in to see your license details.

Search

Popular Resources

Related Posts:

Comment Section:

27 Comments. Leave new

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

    Reply
  • 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?

    Reply
  • 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

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

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

    Reply
  • 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?

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

      Reply
  • 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?

    Reply
  • 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?

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

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

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

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

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

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

    Reply
  • 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

    Reply
  • 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,

    Reply

Leave a Reply to Mikko Saari Cancel reply

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

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed