Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T13:51:43.603Z
Answers.Text: 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 :(
Answers.UserDisplayName: The Sweb Site
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T14:10:33.073Z
Answers.Text: You're right, that was incorrect -- I updated it with a better answer.
Answers.UserDisplayName: Peter Tell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T14:17:32.213Z
Answers.Text: 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'....
Answers.UserDisplayName: The Sweb Site
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T14:19:54.200Z
Answers.Text: Can you edit your original question and post your code? I've tested that exact code and it works fine for me.
Answers.UserDisplayName: Peter Tell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T14:22:58.103Z
Answers.Text: It could be that you need extra square brackets around the hash to merge, so it's `merge([{ targetElement: catPar }])`.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T14:23:35.020Z
Answers.Text: @carlcs is right! Typo here that wasn't in my test. Thanks carlcs, updated!
Answers.UserDisplayName: Peter Tell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T14:25:36.610Z
Answers.Text: And you should prevent to pass an array with `'and'` only. So a conditional with `{% if related|length > 1 %}` should be added.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T14:28:49.733Z
Answers.Text: 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 :)
Answers.UserDisplayName: The Sweb Site
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T14:33:00.923Z
Answers.Text: @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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-14T13:10:35.630Z
Answers.LastActivityDate: 2015-04-14T14:22:40.713Z
Answers.LastEditDate: 2015-04-14T14:22:40.713Z
Answers.UserDisplayName: Peter Tell
Body: <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>
  
Tags: <categories><filter>
Title: Filter by multiple categories
ThreadText: # 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](http://buildwithcraft.com/docs/relations#the-relatedTo-param) 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 username_1, 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 username_1, 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**: @username_0 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.  
  
  ---
  
  