6

I have a section with entries which I wish to push to specific parts of the website. I know I could filter them with a Category but the section already has a category so I would like to use checkboxes.

The checkbox only has 3 options so I could like to return those that are checked for a specific section. Is this possible with checkboxes? Been trying this but not getting anything returned:

  {% set entries = craft.entries.section('newsMedia').order('postDate desc').limit(7).search('zone:pupil')%}

1 Answer 1

8

You'd better double check for a typo because you actually got the syntax right for the search parameter.

search('myField:myString') filters all entries where a field with handle "myField" contains a string "myString". What you have should totally work to get the entries with a checkbox field handle "zone" and the value of the checkbox option being "pupil".

If you're sure that there's no typo and you actually have entries that match the criteria, I'd try if it helps to rebuild the search indexes (tool in the CP settings). I actually try to avoid using the search parameter if possible, because things can go wrong with the indexes. Unfortunatelly I don't think there's other ways to filter by a checkbox field.

Other posibillity: If you don't want to paginate the entries or use other methods that are not available on custom arrays of entry models, what about filtering them with a simple conditional:

{% set entries = craft.entries %}

{% for entry in entries if entry.zone.contains('pupil') %}
    {{ entry.title }}
{% endfor %}
2
  • Thanks - rebuilding the index resolved the issue. In this section I am using these entries I only want the latest 7 entries. Which method uses least resources? Doing the search parameter as I had originally done or using the other option you have? Then exiting the loop when 7 entries are listed,
    – mmc501
    Commented Aug 18, 2014 at 10:41
  • If you use search(), you'll risk running into that indexes issue again. I'd avoid it if the other solution is an option for you, @mmc501.
    – carlcs
    Commented Aug 18, 2014 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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