All Low add-ons are now owned by EEHarbor. Read the blog post.

Support archive

Trying to show entries that have all of the selected categories only

Brian Rivet 23 Sep 2016 15:07 problem, complete

Hi,

I'm having some trouble getting the results I want from Low Search. I'm sure it can do this, I just don't think I have the parameters set correctly. I'm trying to get results where the entries have all of the selected categories only. Meaning, if they select 3 categories and an entry has 2/3 it does not get displayed. Here's my template code:


{exp:low_search:form result_page="searchtest/results" form_id="beersearch"}
<fieldset>
<p>
{exp:channel:categories category_group="2" style="linear"}
<label>
<input type="checkbox" name="beertype[]" value="{category_id}"{if low_search_category ~ '/\b'.category_id.'\b/'} checked{/if}>
{category_name}
</label>
{/exp:channel:categories}
</p>

<p>
{exp:channel:categories category_group="1" style="linear"}
<label>
<input type="checkbox" name="beerstyles[]" value="{category_id}"{if low_search_category ~ '/\b'.category_id.'\b/'} checked{/if}>
{category_name}
</label>
{/exp:channel:categories}
</p>
<input type="hidden" name="category[]" id="categories" value="">
<input type="hidden" name="limit" value="500">
<input type="hidden" name="require_all" value="category">
<input type="hidden" name="orderby_sort" value="title|desc">
<p><button name="submit" id="submit">Search</button></p>
</fieldset>
{/exp:low_search:form}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#submit').click(function(e) {
var mycategories = "";
$('input[name="beertype[]"]:checked').each(function() {
mycategories = mycategories + this.value + ',';
});

$('input[name="beerstyles[]"]:checked').each(function() {
mycategories = mycategories + this.value + ',';
});

$('#categories').val(mycategories);
return true;
});

});
</script>


and here's my results template:


{exp:low_search:results
query="{segment_3}"
channel="beer_brands"
}
{if low_search_no_results}No results found{/if}

{if count == 1}
<p>Total Results: {total_results}</p>
<ul>{/if}
<li><a href="{auto_path}">{title}</a></li>
{if count == total_results}</ul>{/if}

{paginate}
{current_page}/{total_pages} | {pagination_links}
{/paginate}
{/exp:low_search:results}


To try it out you can go to http://mockler.fusesense.com/searchtest

If you go and you click only on Amber Ale as a category, you get 15 results.

If you click only on Craft as a category, you get 202 results.

If you click on Craft and Amber Ale, it returns 202 results again, making me think that it is returning all of the craft selections, and all of the amber ales just happen to be craft also. What I would like is for it to only return entries that are in all of the selected categories, which in this example I would think would return no more than 15 results.

Can you help me to sort out what I am doing wrong? I also plan to have other fields checking other parameters, and I want the results to only show entries that meet all of he selected conditions. For example, I may want items that meet certain categories and have a value within a certain range for some number fields. Is this something Low Search can do?

Thanks,

Brian

Replies

  1. Low 23 Sep 2016 15:57

    You're using JavaScript to populate the hidden category parameter, but you're separating the values with commas, rather than vertical bars (pipes) |.

    Then again, you don't need JavaScript at all. Just use two category groups, for example: http://pastebin.com/RgkXCN4T

  2. Brian Rivet 23 Sep 2016 16:26

    Thanks Low! That worked perfectly. Now, one more question, if I have some additional fields I want to match that are number ranges, how would I do that?

    For example, if I want to show all beers that are in the craft and amber ale categories with an abv that is greater than 1.0 how would I do that? the entries each have an abv field and an ibu field that are number fields.

    Thanks!

  3. Brian Rivet 23 Sep 2016 17:52

    Nevermind the second question. I was able to figure it out. Thanks so much for your help!