<p>Craft's <a href="http://buildwithcraft.com/docs/relations#the-relatedTo-param" rel="nofollow">relatedTo</a> takes a model, an ID, or an array of either of these. So if you're passing <code>'171,172,179'</code> It's probably coming through as a string and only reading the first ID.</p>

<p>Try making it an array and looping through the values:</p>

<pre><code>{% set catPars = craft.request.getParam('cats')|split(',') %}
{% set related = ['and'] %}
{% for catPar in catPars %}
    {% set related = related|merge([{targetElement: catPar}]) %}
{% endfor %}


{% set swatches = craft.entries({ section: 'swatches', relatedTo: related }) %}
</code></pre>

<p>Without knowing your setup I don't know if it's actually <code>targetElement</code> or <code>sourceElement</code>. You can also pass in the <code>field</code> name as well if you want to get more specific. See the documentation linked above for the definitions of all of those.</p>

CC BY-SA 3.0
2015-04-14T13:51:43.603Z
Hi Peter, thank you for the reply. I have tried a range of methods using the guidelines but none seem to help. When I try your method I get no entries at all :(
The Sweb Site
CC BY-SA 3.0
2015-04-14T14:10:33.073Z
You're right, that was incorrect -- I updated it with a better answer.
Peter Tell
CC BY-SA 3.0
2015-04-14T14:17:32.213Z
Hi Peter, I actually used your first answer to do exactly this. However 'catPar' fails to add to the array so we end up with one array entry of 'and'....
The Sweb Site
CC BY-SA 3.0
2015-04-14T14:19:54.200Z
Can you edit your original question and post your code? I've tested that exact code and it works fine for me.
Peter Tell
CC BY-SA 3.0
2015-04-14T14:22:58.103Z
It could be that you need extra square brackets around the hash to merge, so it's `merge([{ targetElement: catPar }])`.
carlcs
CC BY-SA 3.0
2015-04-14T14:23:35.020Z
@carlcs is right! Typo here that wasn't in my test. Thanks carlcs, updated!
Peter Tell
CC BY-SA 3.0
2015-04-14T14:25:36.610Z
And you should prevent to pass an array with `'and'` only. So a conditional with `{% if related|length > 1 %}` should be added.
carlcs
CC BY-SA 3.0
2015-04-14T14:28:49.733Z
Thanks guys that's perfect. It now works perfectly. I will add a condition so that 'and' only get added if required. My Twig syntax knowledge needs some work, so I really appreciate the help :)
The Sweb Site
CC BY-SA 3.0
2015-04-14T14:33:00.923Z
@TheSwebSite you could do it like this: `{% set related = (related|length > 1) ?: '' %}`. This before `{% set swatches = ... %}` and you should always have a correct `relatedTo` argument, even if no category is passed. It should returns all "swatches" entries then.
carlcs
CC BY-SA 3.0
2015-04-14T13:10:35.630Z
2015-04-14T14:22:40.713Z
2015-04-14T14:22:40.713Z
Peter Tell
<p>I need to filter a sections entries by more than one category but only return entries that belong to all category id's passed in a URL parameter.</p>

<p>Now I can get the correct output if the id's are hard coded but not if contained in the URL parameter.</p>

<p>URL: ajax-swatches?cats=171,172,179</p>

<p>{% set catPars = craft.request.getParam('cats') %}</p>

<p>{% set swatches = craft.entries({
    section: 'swatches',
    relatedTo: ['and',catPars]
}) %}</p>

<p>I presume the 'relatedTo' section is treating the 'catPars' variable as a single entry and ignoring the ',' and next values?</p>

<p>Any help would be really appreciated :)</p>

<categories><filter>
Filter by multiple categories
# Filter by multiple categories

(Asked by: username_0 on 2015-04-14)

I need to filter a sections entries by more than one category but only return entries that belong to all category id's passed in a URL parameter.

Now I can get the correct output if the id's are hard coded but not if contained in the URL parameter.

URL: ajax-swatches?cats=171,172,179

{% set catPars = craft.request.getParam('cats') %}

{% set swatches = craft.entries({
section: 'swatches',
relatedTo: ['and',catPars]
}) %}

I presume the 'relatedTo' section is treating the 'catPars' variable as a single entry and ignoring the ',' and next values?

Any help would be really appreciated :)

**Tags:** categories, filter


---

## 1 answers

### Answer by username_1 on 2015-04-14. Score: 4 | Accepted answer

Craft's relatedTo takes a model, an ID, or an array of either of these. So if you're passing '171,172,179' It's probably coming through as a string and only reading the first ID.
Try making it an array and looping through the values:
{% set catPars = craft.request.getParam('cats')|split(',') %}
{% set related = ['and'] %}
{% for catPar in catPars %}
    {% set related = related|merge([{targetElement: catPar}]) %}
{% endfor %}


{% set swatches = craft.entries({ section: 'swatches', relatedTo: related }) %}

Without knowing your setup I don't know if it's actually targetElement or sourceElement. You can also pass in the field name as well if you want to get more specific. See the documentation linked above for the definitions of all of those.

> **Comments:**
> **username_0**: Hi Peter, thank you for the reply. I have tried a range of methods using the guidelines but none seem to help. When I try your method I get no entries at all :(  
> **username_1**: You're right, that was incorrect -- I updated it with a better answer.  
> **username_0**: Hi Peter, I actually used your first answer to do exactly this. However 'catPar' fails to add to the array so we end up with one array entry of 'and'....  
> **username_1**: Can you edit your original question and post your code? I've tested that exact code and it works fine for me.  
> **username_2**: It could be that you need extra square brackets around the hash to merge, so it's `merge([{ targetElement: catPar }])`.  
> **username_1**: @username_2 is right! Typo here that wasn't in my test. Thanks username_2, updated!  
> **username_2**: And you should prevent to pass an array with `'and'` only. So a conditional with `{% if related|length > 1 %}` should be added.  
> **username_0**: Thanks guys that's perfect. It now works perfectly. I will add a condition so that 'and' only get added if required. My Twig syntax knowledge needs some work, so I really appreciate the help :)  
> **username_2**: @TheSwebSite you could do it like this: `{% set related = (related|length > 1) ?: '' %}`. This before `{% set swatches = ... %}` and you should always have a correct `relatedTo` argument, even if no category is passed. It should returns all "swatches" entries then.  

---

