1

I've set up checkbox fields for photos in a photo gallery called "photoColor", "photoSubject", and "photoSize", all of which contain a bunch of options.

On the gallery page I have filters and I'm using Ajax to update the page when a checkbox is clicked.

<div id="filters">
 <input type="checkbox" name="colors[]" value="red" /> Red
 <input type="checkbox" name="colors[]" value="green" /> Green

 <input type="checkbox" name="subjects[]" value="human" /> Human
 <input type="checkbox" name="subjects[]" value="animal" /> Animal

 <input type="checkbox" name="sizes[]" value="small" /> Small
 <input type="checkbox" name="sizes[]" value="large" /> Large
</div>

I'm gathering the values of the checkboxes like this

  var colors = [], subjects = [], sizes = [];
  $('input[name="colors[]"]:checked').each(function() {
     colors.push($(this).val());
  });

  $('input[name="subjects[]"]:checked').each(function() {
     subjects.push($(this).val());
  });

  $('input[name="sizes[]"]:checked').each(function() {
     sizes.push($(this).val());
  });

Right now I can filter for one option of one field type, i.e. this works:

{%  set galleries = craft.entries.section("gallery").photoColor(value) %}

(where value = "red" for example)

But I'm struggling to figure out how to write the query to combine ALL 3 field types in an "or" query, i.e. the results only need to match one of those properties, not all of them.

In other words, if a user checks off red, green, animal, and small, it will show all the results matching either red, green, animal, OR small - not just red/green animals that are small.

In pseudo code, it would be something like:

{% set galleries = craft.entries.section("gallery")
  where .photoColor is red or green
   or .photoSubject is animal
   or .photoSize is small
%}

I've been searching StackExchange and trying things I saw in other answers for days, but none of the situations were quite like mine, so I haven't been able to get it to work. Can someone point me in the right direction? Any help is greatly appreciated.

  • Are the 'colours', 'subjects' and 'sizes' categories in Craft? – dmatthams Jun 24 at 9:14
  • No, they are just set up as checkbox fields. I haven't used the category field type yet. I just looked at it. I'm not sure how to add all the values like I did with the checkbox fields. I"ll have to look into the documentation on that. – Jake1293 Jun 24 at 12:55
2

Apologies if I’ve misunderstood your question and over-simplified things, but wouldn’t the search() method give you precisely what you need?

As in:

{% set valsToFind = "field_photoColor:red OR field_photoColor:green OR field_photoSubject:animal OR field_photoSize:small" %}
{% set galleries = craft.entries.section("gallery").search(valsToFind).all() %}
| improve this answer | |
  • Unfortunately, that doesn't return any results. Even if I simplify it to just one field, i.e. {% set galleries = craft.entries.section("gallery").search("field_photoColor:red") %} there still aren't any results. – Jake1293 Jun 24 at 23:36
  • Try just "red" as in {% set galleries = craft.entries.section("gallery").search("red").all() %} – Mitrol Jun 25 at 0:02
  • That was it, I needed to structure the query like this: {% set galleries = craft.entries.section("gallery").search("red OR green OR animal OR large"). Thank you so much! I've been stuck on this for days. – Jake1293 Jun 25 at 0:17
  • Awesome, glad it worked. Looking at the code in the light of day, the field names are probably wrong. {% set galleries = craft.entries.section("gallery").search("photoColor:red") %} should also work. Cheers. – Mitrol Jun 25 at 13:40
1

I think this will get you there.

{% set galleries = craft.entries
    .section("gallery")
    .where([
        'or',
        {'field_photoColor'   : 'red' },
        {'field_photoColor'   : 'green' },
        {'field_photoSubject' : 'animal' },
        {'field_photoSize'    : 'small' }
    ])
%}
| improve this answer | |
  • Unfortunately, it's not returning any results. I tried with an easy example using only one field, photoColor. – Jake1293 Jun 24 at 13:01
  • Then it's likely you have a misnamed field or typo some where in your implementation, because that code queries data from entries in exactly the way you're looking for. – stevecomrie Jun 24 at 16:33
  • Hmmm...I'm not sure what I'm doing wrong. I double checked all the handles, they're all correct. – Jake1293 Jun 24 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.