<p>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.</p>

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

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

<p><strong>Update</strong> If <a href="https://craftcms.stackexchange.com/questions/1979/can-entries-be-returned-based-on-checkbox-selection">this answer</a> is any indication it also looks like you could use the <code>search</code> method. Using this method, however, may not be 100% reliable if the search index becomes outdated for whatever reason.</p>

<pre><code> {% set entries = craft.entries.section('news').search('category:' ~ craft.request.getSegment(2)) %}
</code></pre>

CC BY-SA 3.0
2015-01-28T12:31:29.590Z
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
CC BY-SA 3.0
2015-01-28T18:31:08.660Z
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
CC BY-SA 3.0
2015-01-28T00:42:45.067Z
2015-01-28T08:59:19.703Z
2017-04-13T12:51:00.200Z
Douglas McDonald
<p>I successfully managed to set this up with Douglas' first solution above combined with another answer also from Douglas about <a href="https://craftcms.stackexchange.com/a/6571/828">array merging</a>.</p>

<p>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.</p>

<p>By adding the merge I ended up with this working solution:</p>

<pre><code>{% 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 %}
</code></pre>

<p>Any suggestions for further shortening this code is welcome.</p>

CC BY-SA 3.0
2015-01-28T12:28:43.080Z
2015-01-28T12:55:12.767Z
2017-04-13T12:51:00.200Z
Alf Vestre
<p>I have succsefully managed to filter entries by matching an url segment to a Radio Buttons Field in my entries, like this:
(here <strong>category</strong> is the field handle of my Radio Buttons field)</p>

<pre><code>{% set filteredEntries = craft.entries.section('news').category(craft.request.getSegment(2)) %}
</code></pre>

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

<pre><code>{% set filteredEntries = craft.entries.section('news').category.contains(craft.request.getSegment(2)) %}
</code></pre>

<p>But it doesn't work. Any suggestions?</p>

<entries><filter><getsegment>
Filter entries by matching an url segment to Checkboxes Field values
# Filter entries by matching an url segment to Checkboxes Field values

(Asked by: username_0 on 2015-01-27)

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?

**Tags:** entries, filter, getsegment


---

## 2 answers

### Answer by username_1 on 2015-01-28. Score: 2 | Accepted answer

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)) %}

> **Comments:**
> **username_0**: 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?  
> **username_2**: 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.  

---

### Answer by username_0 on 2015-01-28. Score: 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.


---

