Low Search Multiple Categories
I've been trying at this for ages and reading your docs, but still haven't got this working yet.
First of all, I want my search results page to be the same as my search page. Is this allowed? I set it so that if its segment_2 it will show the full channel listings but if its segment_3 it will load the filtered search results.
I always get no results.
Also, instead of the typical results code in the URL, is it possible to use the category names? I have two groups: type and sector, so it could show: page/sector/type
Any idea what I'm doing wrong?
My code is on pastie: http://pastie.org/6405600
Replies
Low 6 Mar 2013 18:19
You can use the Results tag as a substitute for the channel:entries tag. Thing to remember is when the query="" parameter is present, a search is assumed and will trigger no_results when the parameter is empty. Use something like {if segment_3}query="{segment_3}"{/if} in the opening tag to avoid that.
Low Search will always work with an encoded query in the URI, so using category names isn't possible. You can change the result page to whatever you'd like, possibly even in a drop down, if that helps.
As for searching categories: you've got 2 single drop-downs in your form, so you're always searching for 2 categories. You're not searching for multiple categories per group. So, since it's limited to just two categories, you can use name="category[]" for both selects.
If you do, then you'll get entries that have either of the two categories assigned, just like defining category="1|2" in the Results (or channel:entries) tag. If you want to change that to the equivalent of category="1&2", add this to the Form tag: require_all="category".
Also, having search:category="sector|type" in the Results tag looks unnecessary, unless you have a custom field called "category". You don't need to add it if you want to filter by category.
Hope that helps.
Paul 6 Mar 2013 18:43
Hi Lowe,
Thanks for your quick response.
I amended the code as you suggested above. However, it's still not working, it won't list any results. http://pastie.org/6405906
How can I set both lists to show "All" by default so when you land on the page it will list all the entries.
Also, for the results, I just have {low_search_categories} in twice. How do I split them out so the two different categories will be listed?
Many thanks.
Low 6 Mar 2013 19:19
The Form tag is getting better. Try this:
This should result in a URL like this: domain.com/projects/index/ABC
...where ABC is the encoded query.
Then use this as the Results tag:
Also, the {low_search_categories} variable inside the Results tag will not do anything. It's {low_search_category} (reflecting the input fields without []), and will contain a pipe-separated list of the category IDs that were selected. If you want to display the category names themselves, you'll need to use a category tag like {exp:channel:categories} or use the {categories} variable pair, since you're already displaying entries.
Paul 6 Mar 2013 19:40
Thanks for your help so far, Lowe. I feel like we're so close to having this work.
I updated the code, the entries now appear instead of getting the no_results message. The two filters still aren't having any affect on the listings, though.
Would you have a quick look at this: http://pastie.org/6406234
I uploaded a screenshot below so you can see what I'm trying to achieve.
Low 6 Mar 2013 20:07
Time to break out the template debugger. Enable it and copy all lines over that mention Low Search for the filter that doesn't trigger any results.
Paul 6 Mar 2013 20:19
Wow, that's cool, never used the debugger before.
Here is the main block that mentions Low Search: http://pastie.org/6406502
Low 6 Mar 2013 20:31
That'll be a non-search page. What about the page where you've submitted the form?
Paul 6 Mar 2013 20:42
That is the page, there isn't really any other information I can find.
Sorry for taking up so much of your time... maybe the best thing, if you don't mind, is to connect to my computer for a minute and have a look? Would that be possible? TeamViewer or something.
Low 6 Mar 2013 20:53
Try switching the segment_3 with freebie_3 vars, since you're using that (and you're probably using Structure).
Otherwise, send me superadmin login credentials to hi at gotolow dot com and I'll take a look tomorrow (it's close to 10pm here now).
Paul 6 Mar 2013 21:00
You're right on both counts. I'll try that and if I can't get it to work I'll send you the login again.
Many thanks.
Low 8 Mar 2013 10:39
Right, I changed a couple of things. This is the code that seems to be working okay: http://pastie.org/6419642
Things I changed, most importantly:
- Added value="" to the All options to prevent category="All" instead of category="";
- Changed the Results tag from query="freebie_2" to query="{freebie_2}" to get the actual value from the URI instead of the literal string "freebie_2".
Other changes, more for finetuning:
- Set the default result page to "projects" in the extension settings so I could remove the attribute from the Form tag, resulting in a shorter URL;
- Removed the collection="" parameter from the Form tag as there was only 1 (omitting it searches all collections), resulting in a shorter URL;
- Moved the require_all parameter to the Results tag, resulting in a shorter URL;
- Changed the GWcode categories tag to a native {categories} variable pair for better performance;
- Added the disable="" parameter to the Results tag for better performance.
The search now works properly. However, the results may be a bit confusing, as a Project can be assigned to multiple categories per type. Only the first one is shown in your list. The filtering, however, is correct.
Paul 8 Mar 2013 11:11
Ahhh, I see where I went wrong in a couple of small areas.
Low, you are AWESOME. Thank you so much. I agree it may get confusing as a project can be in multiple categories but that's okay for now.