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.
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,
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
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.
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.
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.
Can’t wait to see what the widget looks like
I know I’m asking too much… but here’s some inspiration for your widget:
http://www.ebay.com/sch/ebayadvsearch/?_skipfnorm=1&rt=nc
I’d like to have radio buttons to choose multiple categories. Wondering if your current plug-in could be modified for this?
Go ahead and use radio buttons, no problems with that. Relevanssi doesn’t care what kind of user interface element is used.
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.
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.
Is there a way to share this ‘author filter’ code/form in the knowledge base?
Thank you!
It is very simple: just replace “cat” with “author” in the example and the category ID number with the author ID number.
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?
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.
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?
That is already available in the Premium version.
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?
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.
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.
Well, it is slightly complicated, so I’d have to think about how to do it, no instant solution available.
No need to do that! Thank you anyway, it’s a fundamental plugin for us.
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
Kent, see this knowledge base entry, and in particular the comment by Emily Johnson: https://www.relevanssi.com/knowledge-base/search-results-breakdown-by-type/
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?
Sure. Check this out: https://www.relevanssi.com/knowledge-base/search-results-breakdown-by-type/ It’s a breakdown by type, but it’s not hard to change to use categories.
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.
Yes, you can filter by two categories, but it requires some programming, you have to combine the two category parameters to one tax_query.