Answers.Body: <p>This should do it:</p>
  
  <pre><code>{% set eventCats = craft.request.getParam('cat')|split(',') %}
  {% set events = craft.entries.relatedTo(['and']|merge(eventCats)).find() %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-25T00:34:14.147Z
Answers.Text: I'm getting an "Argument #2 is not an array" error - eventCats is definitely an array, is this a syntax error?
Answers.UserDisplayName: Matthew Rotter
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-24T21:29:14.720Z
Answers.LastActivityDate: 2018-09-24T21:29:14.720Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <h3>Answer for Craft 2</h3>
  <p>Your final result should not look like <code>relatedTo('and', '1,2')</code> but</p>
  <pre><code>relatedTo('and, 1, 2')
  </code></pre>
  <p>Or</p>
  <pre><code>relatedTo(['and', 1, 2])
  </code></pre>
  <p>So you just need to do</p>
  <pre><code>relatedTo('and,' ~ param)
  </code></pre>
  <p>Keep in mind to code defensively, so you might want to check if your query param is not null or something</p>
  <h3>Answer for Craft 3</h3>
  <pre><code>relatedTo(['and', ['element' =&gt; 1], ['element' =&gt; 2]])
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-25T01:30:19.323Z
Answers.Text: "Argument #2 is not an array" for any of these syntaxes. Is it because of how craft 3 has changed the relations syntax? Or am I being a goober somehow?
Answers.UserDisplayName: Matthew Rotter
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-24T21:28:51.493Z
Answers.LastActivityDate: 2018-09-25T14:22:47.520Z
Answers.LastEditDate: 2020-06-17T08:56:27.903Z
Answers.UserDisplayName: Robin Schambach
Answers.Body: <pre><code>{% set eventCats = craft.request.getParam('cat') %}
  {% set eventCats = eventCats|split(',') %}
  
  {% set cats = [] %}
  
  {% for eventCat in eventCats %}
      {% set cats = cats|merge([{element: eventCat}]) %}
  {% endfor %}
  
  {% if cats|length &gt; 0 %}
      {% set cats = ['and']|merge(cats) %}
  {% endif %}
  
  {% set events = craft.entries.relatedTo(cats) %}
  </code></pre>
  
  <p>Shout out to Stephen Hamilton who came up with this answer. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-25T14:34:38.817Z
Answers.LastActivityDate: 2018-09-25T14:34:38.817Z
Answers.UserDisplayName: Matthew Rotter
Body: <p>I'm creating a filter and would like to return entries that must contain multiple categories (and behavior, not or). A sample url would be /events?cat=2,3. </p>
  
  <pre><code>{% set eventCat = craft.request.getParam('cat') %}
  {% set events = craft.entries.relatedTo(eventCat).all() %}
  </code></pre>
  
  <p>Gives me "or" behavior and </p>
  
  <pre><code>{% set eventCat = craft.request.getParam('cat') %}
  {% set events = craft.entries.relatedTo('and', eventCat).all() %}
  </code></pre>
  
  <p>Doesn't give me any results.</p>
  
  <p><strong>(updated to reflect Craft 3 syntax, thanks Robin!)</strong></p>
  
Tags: <craft3><categories><relatedto><query>
Title: Returning entries that contain both categories
ThreadText: # Returning entries that contain both categories
  
  (Asked by: username_0 on 2018-09-24)
  
  I'm creating a filter and would like to return entries that must contain multiple categories (and behavior, not or). A sample url would be /events?cat=2,3.
  
  ```
  {% set eventCat = craft.request.getParam('cat') %}
  {% set events = craft.entries.relatedTo(eventCat).all() %}
  
  ```
  
  Gives me "or" behavior and
  
  ```
  {% set eventCat = craft.request.getParam('cat') %}
  {% set events = craft.entries.relatedTo('and', eventCat).all() %}
  
  ```
  
  Doesn't give me any results.
  
  **(updated to reflect Craft 3 syntax, thanks username_2!)**
  
  **Tags:** craft3, categories, relatedto, query
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_0 on 2018-09-25. Score: 0 | Accepted answer
  
  ```
  {% set eventCats = craft.request.getParam('cat') %}
  {% set eventCats = eventCats|split(',') %}
  
  {% set cats = [] %}
  
  {% for eventCat in eventCats %}
      {% set cats = cats|merge([{element: eventCat}]) %}
  {% endfor %}
  
  {% if cats|length > 0 %}
      {% set cats = ['and']|merge(cats) %}
  {% endif %}
  
  {% set events = craft.entries.relatedTo(cats) %}
  
  ```
  
  Shout out to Stephen Hamilton who came up with this answer.
  
  
  ---
  
  ### Answer by username_1 on 2018-09-24. Score: 3
  
  This should do it:
  
  ```
  {% set eventCats = craft.request.getParam('cat')|split(',') %}
  {% set events = craft.entries.relatedTo(['and']|merge(eventCats)).find() %}
  
  ```
  
  > **Comments:**
  > **username_0**: I'm getting an "Argument #2 is not an array" error - eventCats is definitely an array, is this a syntax error?  
  
  ---
  
  ### Answer by username_2 on 2018-09-24. Score: 3
  
  ### Answer for Craft 2
  
  Your final result should not look like `relatedTo('and', '1,2')` but
  
  ```
  relatedTo('and, 1, 2')
  
  ```
  
  Or
  
  ```
  relatedTo(['and', 1, 2])
  
  ```
  
  So you just need to do
  
  ```
  relatedTo('and,' ~ param)
  
  ```
  
  Keep in mind to code defensively, so you might want to check if your query param is not null or something
  
  ### Answer for Craft 3
  
  ```
  relatedTo(['and', ['element' => 1], ['element' => 2]])
  
  ```
  
  > **Comments:**
  > **username_0**: "Argument #2 is not an array" for any of these syntaxes. Is it because of how craft 3 has changed the relations syntax? Or am I being a goober somehow?  
  
  ---
  
  