Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-28T17:16:26.560Z
Answers.Text: Hmm okay thanks - is there any way I can loop through categories, since it can contain 1 or more categories depending on parameters
Answers.UserDisplayName: Daniel
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-28T17:23:44.010Z
Answers.Text: @Daniel Sure, just updated the answer.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-11T21:22:20.213Z
Answers.Text: @BrandonKelly Does the original relatedTo syntax no longer work in 2.5?
Answers.UserDisplayName: jthomas
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-11T21:23:52.193Z
Answers.Text: @jthomas it does, the new syntax is supported in addition to precious syntaxes.
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-11T21:52:37.780Z
Answers.Text: @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
Answers.UserDisplayName: jthomas
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-11T22:22:07.153Z
Answers.Text: @jthomas Can you please post that code in a new SE question?
Answers.UserDisplayName: Brandon Kelly
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-11-11T22:58:16.697Z
Answers.Text: @BrandonKelly Sure. Here it is. http://craftcms.stackexchange.com/questions/12352/perform-inclusive-relatedto-query-with-multiple-categories-in-craft-2-5
Answers.UserDisplayName: jthomas
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-28T13:00:05.610Z
Answers.LastActivityDate: 2015-11-11T20:23:59.553Z
Answers.LastEditDate: 2015-11-11T20:23:59.553Z
Answers.UserDisplayName: Brandon Kelly
Body: <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>
  
Tags: <templating><category>
Title: Perform "inclusive" relatedTo query with multiple categories
ThreadText: # 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](http://buildwithcraft.com/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**: @username_0 Sure, just updated the answer.  
  > **username_2**: @username_1 Does the original relatedTo syntax no longer work in 2.5?  
  > **username_1**: @username_2 it does, the new syntax is supported in addition to precious syntaxes.  
  > **username_2**: @username_1 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**: @username_2 Can you please post that code in a new SE question?  
  > **username_2**: @username_1 Sure. Here it is. http://craftcms.stackexchange.com/questions/12352/perform-inclusive-relatedto-query-with-multiple-categories-in-craft-2-5  
  
  ---
  
  