Answers.Body: <p>Completely untested, but try something like:</p>
  
  <pre><code>{% set ids = [] %}
  
  {% if craft.request.getQuery('sector') %}
      {% set ids = ids|merge([craft.request.getQuery('sector')]) %}
  {% endif %}
  
  {% if craft.request.getQuery('impact') %}
      {% set ids = ids|merge([craft.request.getQuery('impact')]) %}
  {% endif %}
  
  {% if craft.request.getQuery('topic') %}
      {% set ids = ids|merge([craft.request.getQuery('topic')]) %}
  {% endif %}
  
  {% if ids|length %}
  
      {% for post in craft.entries.section(section).relatedTo('and', ids|join(',')) %}
          ....
      {% endfor %}
  
  {% endif %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-25T05:11:53.483Z
Answers.Text: Very clever solution! Is there a need for `'and'` somewhere in the `categories` value?
Answers.UserDisplayName: Lindsey D
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-25T08:49:07.587Z
Answers.Text: Ah yes that's what I'm looking for - didn't think of the merge function. Will try that shortly.
Answers.UserDisplayName: Daniel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-25T14:03:12.950Z
Answers.Text: Ah yes I spoke too soon: even with `.relatedTo('and', categories)` it performs an "or" query, rather than an "and". Any ideas?
Answers.UserDisplayName: Daniel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-25T14:34:35.420Z
Answers.Text: Updated answer. Again... completely untested, but that should do it.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-26T12:25:52.293Z
Answers.Text: Brad, out of interest, if you've already got the ids as an array, why to you need to query them again with `{% set categoriesIds = craft.categories.id(ids).ids() %}` ?
Answers.UserDisplayName: Jamie Pittock
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-26T14:53:19.320Z
Answers.Text: @JamiePittock Hah... good call.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-25T00:07:24.360Z
Answers.LastActivityDate: 2014-09-26T16:31:46.503Z
Answers.LastEditDate: 2014-09-26T16:31:46.503Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm trying to get all entries that related to one .. three categories, as determined whether their id appears in the query string:</p>
  
  <pre><code>{% set sector = craft.categories.id(craft.request.getQuery('sector')) %}
  {% set impact = craft.categories.id(craft.request.getQuery('impact')) %}
  {% set topic = craft.categories.id(craft.request.getQuery('topic')) %}
  
  {% for post in craft.entries.section(section).relatedTo('and', sector, impact, topic) %}
  </code></pre>
  
  <p>This works well if each of sector, impact, and topic are present, but will return nothing if any one of sector, impact, topic is null (i.e. getQuery('') is blank). Can anyone suggest a way I can do this?</p>
  
Tags: <categories><relations>
Title: Getting entries related to all of supplied categories, if they exist
ThreadText: # Getting entries related to all of supplied categories, if they exist
  
  (Asked by: username_0 on 2014-09-24)
  
  I'm trying to get all entries that related to one .. three categories, as determined whether their id appears in the query string:
  
  ```
  {% set sector = craft.categories.id(craft.request.getQuery('sector')) %}
  {% set impact = craft.categories.id(craft.request.getQuery('impact')) %}
  {% set topic = craft.categories.id(craft.request.getQuery('topic')) %}
  
  {% for post in craft.entries.section(section).relatedTo('and', sector, impact, topic) %}
  
  ```
  
  This works well if each of sector, impact, and topic are present, but will return nothing if any one of sector, impact, topic is null (i.e. getQuery('') is blank). Can anyone suggest a way I can do this?
  
  **Tags:** categories, relations
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-09-25. Score: 5 | Accepted answer
  
  Completely untested, but try something like:
  
  ```
  {% set ids = [] %}
  
  {% if craft.request.getQuery('sector') %}
      {% set ids = ids|merge([craft.request.getQuery('sector')]) %}
  {% endif %}
  
  {% if craft.request.getQuery('impact') %}
      {% set ids = ids|merge([craft.request.getQuery('impact')]) %}
  {% endif %}
  
  {% if craft.request.getQuery('topic') %}
      {% set ids = ids|merge([craft.request.getQuery('topic')]) %}
  {% endif %}
  
  {% if ids|length %}
  
      {% for post in craft.entries.section(section).relatedTo('and', ids|join(',')) %}
          ....
      {% endfor %}
  
  {% endif %}
  
  ```
  
  > **Comments:**
  > **username_2**: Very clever solution! Is there a need for `'and'` somewhere in the `categories` value?  
  > **username_0**: Ah yes that's what I'm looking for - didn't think of the merge function. Will try that shortly.  
  > **username_0**: Ah yes I spoke too soon: even with `.relatedTo('and', categories)` it performs an "or" query, rather than an "and". Any ideas?  
  > **username_1**: Updated answer. Again... completely untested, but that should do it.  
  > **username_3**: username_1, out of interest, if you've already got the ids as an array, why to you need to query them again with `{% set categoriesIds = craft.categories.id(ids).ids() %}` ?  
  > **username_1**: @username_3 Hah... good call.  
  
  ---
  
  