<p>That <code>and</code> argument only applies to the other top-level relatedTo arguments. If one of those arguments targets multiple elements (e.g. <code>categories</code>), those will still be glued together with “OR”.</p>

<p>You would need to set it up like this instead:</p>

<pre><code>{% set resources = craft.entries.section('resources').relatedTo(
    'and',
    { targetElement: category1 },
    { targetElement: category2 }
) %}
</code></pre>

<p>If you don’t know all of the categories ahead of time, you can create the parameter value dynamically like this:</p>

<pre><code>{# Get the category IDs #}
{% set catIds = craft.categories[...].ids() %}

{# Create the relatedTo param value #}    
{% set relatedTo = ['and'] %}

{% for id in catIds %}
    {% set relatedTo = relatedTo|merge([id]) %}
{% endfor %}

{# Now find our entries #}
{% set resources = craft.entries.section('resources').relatedTo(relatedTo) %}
</code></pre>

<p><strong>Update</strong></p>

<p>This is significantly easier in Craft 2.5 (<a href="http://buildwithcraft.com/beta" rel="nofollow">currently in beta</a>), as the relatedTo param’s <code>element</code>, <code>targetElement</code>, and <code>sourceElement</code> sub-properties can now begin with <code>'and'</code>, which tells Craft to only return elements that are related to <em>all</em> of the following element IDs.</p>

<p>Here’s a quick example:</p>

<pre><code>{# Start with a list of category IDs that must be included #}
{% set catIds = [1, 2] %}

{# Find the entries #}
{% set resources = craft.entries({
    section: 'resources',
    relatedTo: {
        targetElement: ['and']|merge(catIds),
    }
}) %}
</code></pre>

CC BY-SA 3.0
2014-09-28T17:16:26.560Z
Hmm okay thanks - is there any way I can loop through categories, since it can contain 1 or more categories depending on parameters
Daniel
CC BY-SA 3.0
2014-09-28T17:23:44.010Z
@Daniel Sure, just updated the answer.
Brandon Kelly
CC BY-SA 3.0
2015-11-11T21:22:20.213Z
@BrandonKelly Does the original relatedTo syntax no longer work in 2.5?
jthomas
CC BY-SA 3.0
2015-11-11T21:23:52.193Z
@jthomas it does, the new syntax is supported in addition to precious syntaxes.
Brandon Kelly
CC BY-SA 3.0
2015-11-11T21:52:37.780Z
@BrandonKelly Your updated example is returning an "or" scenario for me.  Here is the code I'm using. But manually passing two IDs still gives me entries matching "any" of the category IDs instead of only entries matching "all" category IDs. http://pastebin.com/2HQjrChj
jthomas
CC BY-SA 3.0
2015-11-11T22:22:07.153Z
@jthomas Can you please post that code in a new SE question?
Brandon Kelly
CC BY-SA 3.0
2015-11-11T22:58:16.697Z
@BrandonKelly Sure. Here it is. http://craftcms.stackexchange.com/questions/12352/perform-inclusive-relatedto-query-with-multiple-categories-in-craft-2-5
jthomas
CC BY-SA 3.0
2014-09-28T13:00:05.610Z
2015-11-11T20:23:59.553Z
2015-11-11T20:23:59.553Z
Brandon Kelly
<p>I want to be able to return all entries that are related to 1 .. n categories:</p>

<pre><code>{% set resources = craft.entries.section('resources').relatedTo('and', categories) %}
</code></pre>

<p>in this example, categories contains two categories category1 and category2, and the query returns all entries that have either <strong>category1</strong> or <strong>category2</strong>.</p>

<p>How do I change the syntax to return entries that are related to <em>both</em> <strong>category1</strong> <em>and</em> <strong>category2</strong>? </p>

<p>I thought 'and' would do it, but seemingly not.</p>

<templating><category>
Perform "inclusive" relatedTo query with multiple categories
# Perform "inclusive" relatedTo query with multiple categories

(Asked by: username_0 on 2014-09-25)

I want to be able to return all entries that are related to 1 .. n categories:
{% set resources = craft.entries.section('resources').relatedTo('and', categories) %}

in this example, categories contains two categories category1 and category2, and the query returns all entries that have either category1 or category2.
How do I change the syntax to return entries that are related to both category1 and category2? 
I thought 'and' would do it, but seemingly not.

**Tags:** templating, category

---

## 1 answers

### Answer by username_1 on 2014-09-28. Score: 9

That and argument only applies to the other top-level relatedTo arguments. If one of those arguments targets multiple elements (e.g. categories), those will still be glued together with “OR”.
You would need to set it up like this instead:
{% set resources = craft.entries.section('resources').relatedTo(
    'and',
    { targetElement: category1 },
    { targetElement: category2 }
) %}

If you don’t know all of the categories ahead of time, you can create the parameter value dynamically like this:
{# Get the category IDs #}
{% set catIds = craft.categories[...].ids() %}

{# Create the relatedTo param value #}    
{% set relatedTo = ['and'] %}

{% for id in catIds %}
    {% set relatedTo = relatedTo|merge([id]) %}
{% endfor %}

{# Now find our entries #}
{% set resources = craft.entries.section('resources').relatedTo(relatedTo) %}

Update
This is significantly easier in Craft 2.5 (currently in beta), as the relatedTo param’s element, targetElement, and sourceElement sub-properties can now begin with 'and', which tells Craft to only return elements that are related to all of the following element IDs.
Here’s a quick example:
{# Start with a list of category IDs that must be included #}
{% set catIds = [1, 2] %}

{# Find the entries #}
{% set resources = craft.entries({
    section: 'resources',
    relatedTo: {
        targetElement: ['and']|merge(catIds),
    }
}) %}

> **Comments:**
> **username_0**: Hmm okay thanks - is there any way I can loop through categories, since it can contain 1 or more categories depending on parameters  
> **username_1**: @Daniel Sure, just updated the answer.  
> **username_2**: @BrandonKelly Does the original relatedTo syntax no longer work in 2.5?  
> **username_1**: @jthomas it does, the new syntax is supported in addition to precious syntaxes.  
> **username_2**: @BrandonKelly Your updated example is returning an "or" scenario for me.  Here is the code I'm using. But manually passing two IDs still gives me entries matching "any" of the category IDs instead of only entries matching "all" category IDs. http://pastebin.com/2HQjrChj  
> **username_1**: @jthomas Can you please post that code in a new SE question?  
> **username_2**: @BrandonKelly Sure. Here it is. http://craftcms.stackexchange.com/questions/12352/perform-inclusive-relatedto-query-with-multiple-categories-in-craft-2-5  

---

