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

CC BY-SA 4.0
2018-09-25T00:34:14.147Z
I'm getting an "Argument #2 is not an array" error - eventCats is definitely an array, is this a syntax error?
Matthew Rotter
CC BY-SA 4.0
2018-09-24T21:29:14.720Z
2018-09-24T21:29:14.720Z
Mats Mikkel Rummelhoff
<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>

CC BY-SA 4.0
2018-09-25T01:30:19.323Z
"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?
Matthew Rotter
CC BY-SA 4.0
2018-09-24T21:28:51.493Z
2018-09-25T14:22:47.520Z
2020-06-17T08:56:27.903Z
Robin Schambach
<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>

CC BY-SA 4.0
2018-09-25T14:34:38.817Z
2018-09-25T14:34:38.817Z
Matthew Rotter
<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>

<craft3><categories><relatedto><query>
Returning entries that contain both categories
# 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 Robin!)

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

---

