1

I apologize if this question has been asked before. I'm new at this and am struggling to figure out if what I need to do is possible/the right way to do it or if I need to structure my content differently.

My entries in a section have a field that use a checkbox field type. I would like to display entries that have a matching value based on that field.

Example: Field: Vitamins,

  Field Type: Checkboxes
              Option: Vitamin A  | Value: a
              Option: Vitamin B  | Value: b
              Option: Vitamin C  | Value: c

Section: Vegetables

 -Entry: Broccoli
       Vitamins Field Values: a, c

 -Entry: Corn
       Vitamins Field Values: a, b

 -Entry: Squash
       Vitamins Field Values: a, b, c

I'm trying to figure out how to display all entries that contain Vitamin A.

3

After I posted my question I ended up coming with something that works:

 {% for entry in craft.entries.section('Vegetables') if entry.Vitamins.contains('a') %}
      <p><a href="{{ entry.url }}">{{ entry.title }}</a></p>
 {% endfor %}
  • While this approach works, it seems like we should be able to use the native search() methods and syntax to find entries having checkbox field options checked. – Roger Glenn Oct 6 '15 at 15:49
1

I posted a similar question the other week, I managed to figure it out and I posted my code. Hope it helps:

Filter based Search Form

1

While the approach above works this is how you'd construct a string to use with craft.entries.search() when you have multiple checkbox fields. The benefit is being able to order entries by relevancy/score.

{% set string = '' %}

{% if craft.request.getParam('outcomes') | length %}
    {% for outcome in craft.request.getParam('outcomes') %}
        {% set string = string ~ ' outcomes:' ~ outcome %}
    {% endfor %}
{% endif %}

{% if craft.request.getParam('age') | length %}
    {% for age in craft.request.getParam('age') %}
        {% set string = string ~ ' age:' ~ age %}
    {% endfor %}
{% endif %}

{% if string != '' %}
    {% set entries = craft.entries.section('interventions').search(string).order('score') %}
{% endif %}

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.