<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>

CC BY-SA 3.0
2014-09-25T05:11:53.483Z
Very clever solution! Is there a need for `'and'` somewhere in the `categories` value?
Lindsey D
CC BY-SA 3.0
2014-09-25T08:49:07.587Z
Ah yes that's what I'm looking for - didn't think of the merge function. Will try that shortly.
Daniel
CC BY-SA 3.0
2014-09-25T14:03:12.950Z
Ah yes I spoke too soon: even with `.relatedTo('and', categories)` it performs an "or" query, rather than an "and". Any ideas?
Daniel
CC BY-SA 3.0
2014-09-25T14:34:35.420Z
Updated answer. Again... completely untested, but that should do it.
Brad Bell
CC BY-SA 3.0
2014-09-26T12:25:52.293Z
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() %}` ?
Jamie Pittock
CC BY-SA 3.0
2014-09-26T14:53:19.320Z
@JamiePittock Hah... good call.
Brad Bell
CC BY-SA 3.0
2014-09-25T00:07:24.360Z
2014-09-26T16:31:46.503Z
2014-09-26T16:31:46.503Z
Brad Bell
<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>

<categories><relations>
Getting entries related to all of supplied categories, if they exist
# 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**: 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() %}` ?  
> **username_1**: @JamiePittock Hah... good call.  

---

