1

I have succsefully managed to filter entries by matching an url segment to a Radio Buttons Field in my entries, like this: (here category is the field handle of my Radio Buttons field)

{% set filteredEntries = craft.entries.section('news').category(craft.request.getSegment(2)) %}

After having to change the Radio Buttons Field to a Checkboxes Field I tried adapting my code like this: (here category is the field handle of my Checkboxes Field)

{% set filteredEntries = craft.entries.section('news').category.contains(craft.request.getSegment(2)) %}

But it doesn't work. Any suggestions?

2 Answers 2

2

This is just a guess, as I've never used the 'contains' method before, but it's possible that it can't be used directly with an ElementCriteriaModel query. You might have to filter the entries after the fact.

{% set entries = craft.entries.section('news') %}
{% for entry in entries if entry.category.contains(craft.request.getSegment(2)) %}
    {{ entry.title }}
{% endfor %}

This probably defeats the purpose of using it as a psuedo-category selector however.

Update If this answer is any indication it also looks like you could use the search method. Using this method, however, may not be 100% reliable if the search index becomes outdated for whatever reason.

 {% set entries = craft.entries.section('news').search('category:' ~ craft.request.getSegment(2)) %}
2
  • I don't need to use the 'contains' method, any method that can filter directly on the ElementCriteriaModel query would suffice. Both your solutions work, out of simplicity and short code I prefer the last one. I have read in many posts about the lack of reliability for the search index, maybe @brad-bell can clarify this; can we safely use the search method in ElementCriteriaModel queries?
    – Alf Vestre
    Commented Jan 28, 2015 at 12:31
  • You can use search, sure. But there are many reason that a search index (craft_searchindex table)might become out of date/sync with the data that is in the rest of the tables it is supposed to represent. So if you need 100% accuracy, 100% of the time, then don't use it.
    – Brad Bell
    Commented Jan 28, 2015 at 18:31
1

I successfully managed to set this up with Douglas' first solution above combined with another answer also from Douglas about array merging.

Without this merge I had to repeat all my template code first in a 'for' loop without a request segment and then again inside a 'for' loop with a request segment.

By adding the merge I ended up with this working solution:

{% if craft.request.getSegment(2) %}
    {% set newsAll = craft.entries.section('news') %}
    {% set news = [] %}
    {% for entry in newsAll if entry.category.contains(craft.request.getSegment(2)) %}
        {% set news = news|merge([entry]) %}
    {% endfor %}
{% else %}
    {% set news = craft.entries.section('news') %}
{% endif %}

{% for entry in news %}
    {# My template code for all or filtered entries #}
{% endfor %}

Any suggestions for further shortening this code is welcome.

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.