<p>It seems that the <code>relatedTo</code> is by default an <code>or</code>, so you only need to define <code>and</code> once when the rest will be considered <code>or</code> e.g.</p>
<pre><code>$query = Entry::find();

// Convert these IDs into Entries/Category models first.
$foodTypes = [1, 3, 5];
$weathers = [7, 4];

$query-&gt;relateTo([
    'and',
    ['targetElement' =&gt; $foodTypes],
    ['targetElement' =&gt; $weathers],
]);

return $query-&gt;all();
</code></pre>
<p>This will make sure each array passed into <code>relateTo</code> is considered an <code>and group</code> but the items within that are considered as an <code>or group</code>.</p>

CC BY-SA 4.0
2020-12-18T11:30:35.773Z
2020-12-18T11:30:35.773Z
owenmelbz
<p>We're trying to make a filter that allows you to pick multiple items from 1 category, and multiple items from another category and return back all appropriate elements.</p>
<p>We're in PHP land not twig so the starting point we have is...</p>
<pre><code>
$results = Entry::find()
    -&gt;type('recipe')
    -&gt;relatedTo($conditions)
    -&gt;all();
</code></pre>
<p>The pseudo query would be...</p>
<pre><code>SELECT * FROM `entries`
WHERE
 (`food_type` = &quot;cake&quot; or `category` = &quot;burger&quot;)
AND
(`weather` = &quot;summer&quot; or `tag` is &quot;hot&quot;)
</code></pre>
<p>I've got the IDs and the hydrated models/elements of each category but can't figure out how to nest the conditions using the <code>relatedTo</code> method on the query..</p>
<p>Thanks</p>

CC BY-SA 4.0
2020-12-18T09:10:03.303Z
Here's how to do it in Twig: https://craftcms.stackexchange.com/a/36309/271 - You can probably take that logic and convert it fairly easily into PHP.
James Smith
CC BY-SA 4.0
2020-12-18T11:25:56.640Z
Hmmm I'd seen similar ones but the `and` is confusing as it never states `or` anywhere, I guess this is maybe then implied... thanks anyway
owenmelbz
<query><relatedto><nested>
Multiple relatedTo conditions using a combination of "and" and "or"
# Multiple relatedTo conditions using a combination of "and" and "or"

(Asked by: username_0 on 2020-12-17)

We're trying to make a filter that allows you to pick multiple items from 1 category, and multiple items from another category and return back all appropriate elements.
We're in PHP land not twig so the starting point we have is...

$results = Entry::find()
    ->type('recipe')
    ->relatedTo($conditions)
    ->all();

The pseudo query would be...
SELECT * FROM `entries`
WHERE
 (`food_type` = "cake" or `category` = "burger")
AND
(`weather` = "summer" or `tag` is "hot")

I've got the IDs and the hydrated models/elements of each category but can't figure out how to nest the conditions using the relatedTo method on the query..
Thanks

**Tags:** query, relatedto, nested
> **Comments:**
> **username_1**: Here's how to do it in Twig: https://craftcms.stackexchange.com/a/36309/271 - You can probably take that logic and convert it fairly easily into PHP.  
> **username_0**: Hmmm I'd seen similar ones but the `and` is confusing as it never states `or` anywhere, I guess this is maybe then implied... thanks anyway  

---

## 1 answers

### Answer by username_0 on 2020-12-18. Score: 2

It seems that the relatedTo is by default an or, so you only need to define and once when the rest will be considered or e.g.
$query = Entry::find();

// Convert these IDs into Entries/Category models first.
$foodTypes = [1, 3, 5];
$weathers = [7, 4];

$query->relateTo([
    'and',
    ['targetElement' => $foodTypes],
    ['targetElement' => $weathers],
]);

return $query->all();

This will make sure each array passed into relateTo is considered an and group but the items within that are considered as an or group.


---

