<p>If I understand properly, you should be able to do this:</p>
<pre class="lang-php prettyprint-override"><code>$entries = \craft\elements\Entry::find()
    -&gt;section('yourSection')
    -&gt;category(['and', 695])
    -&gt;relatedTo([698,698])-&gt;all();
</code></pre>
<p>Assuming <code>category</code> is the name of your relational field, you can use that to pass the <code>and</code> condition (though with one element only it wouldn't be needed) then <code>relatedTo</code> is an <code>or</code> by default (<a href="https://docs.craftcms.com/v3/relations.html#simple-relationships" rel="nofollow noreferrer">docs</a>)</p>
<p>And as per your edit, pass <code>and</code> condition to your custom fied and <code>or</code> conditions to your <code>relatedTo</code>.</p>
<p>It would be pretty much the same in Twig.</p>

CC BY-SA 4.0
2020-07-03T10:15:17.280Z
Yes! So simple, so effective! And this would work perfectly with my other scenario where there's different operators for different category groups as well. Thanks.
peirix
CC BY-SA 4.0
2020-07-02T12:29:40.583Z
2020-07-03T10:37:02.460Z
2020-07-03T10:37:02.460Z
Oli
<p>Not able to comment, so trying an answer.
Did you tried to build a custom hierarchy by scrolling the content of each level in a custom object ?</p>

CC BY-SA 4.0
2020-07-02T10:35:15.487Z
Hi. Not sure what you mean. Could you explain a bit more? What kind of custom object and custom hierarchy?
peirix
CC BY-SA 4.0
2020-07-02T06:17:44.000Z
2020-07-02T06:17:44.000Z
Amaury Van Espen
<p>I have a category with 2 levels. And I want to be able to let the user do a search based on those. The problem is that it needs to be <code>AND</code> between all level 1 categories, and <code>OR</code> within child categories. So let's say I have a category structure like this:</p>
<pre><code>- Elevator
- Air condition
- View
  - Ocean
  - Mountains
  - Garden
</code></pre>
<p>Now when a user selects <code>elevator</code>, <code>ocean</code> and <code>mountains</code> I want to return all entries that have <code>elevator AND (ocean OR mountains)</code></p>
<p>I tried this: <code>$entries-&gt;categories(['and', 695, ['or', 698, 697]]);</code>, but that doesn't work, as it seems to just skip the <code>['or', 698, 697]</code> part.</p>
<p><strong>Edit for bounty:</strong> In addition to the reason given above, there is also a change in the requirement since we added multiple categories. So for one category group, the operator needs to be <code>OR</code>, and some groups should have <code>AND</code>.</p>

CC BY-SA 4.0
2020-06-30T16:05:50.227Z
I take it you want to do this in a plugin, not twig?
dmatthams
CC BY-SA 4.0
2020-07-01T06:09:00.257Z
Yeah. But I guess it would be very similar solution?
peirix
<elementquery><yii>
Combine AND and OR in a ElementQuery
# Combine AND and OR in a ElementQuery

(Asked by: username_0 on 2020-01-15)

I have a category with 2 levels. And I want to be able to let the user do a search based on those. The problem is that it needs to be AND between all level 1 categories, and OR within child categories. So let's say I have a category structure like this:
- Elevator
- Air condition
- View
  - Ocean
  - Mountains
  - Garden

Now when a user selects elevator, ocean and mountains I want to return all entries that have elevator AND (ocean OR mountains)
I tried this: $entries->categories(['and', 695, ['or', 698, 697]]);, but that doesn't work, as it seems to just skip the ['or', 698, 697] part.
Edit for bounty: In addition to the reason given above, there is also a change in the requirement since we added multiple categories. So for one category group, the operator needs to be OR, and some groups should have AND.

**Tags:** elementquery, yii

> **Comments:**
> **username_1**: I take it you want to do this in a plugin, not twig?  
> **username_0**: Yeah. But I guess it would be very similar solution?  

---

## 2 answers

### Answer by username_2 on 2020-07-02. Score: 3 | Accepted answer

If I understand properly, you should be able to do this:
$entries = \craft\elements\Entry::find()
    ->section('yourSection')
    ->category(['and', 695])
    ->relatedTo([698,698])->all();

Assuming category is the name of your relational field, you can use that to pass the and condition (though with one element only it wouldn't be needed) then relatedTo is an or by default (docs)
And as per your edit, pass and condition to your custom fied and or conditions to your relatedTo.
It would be pretty much the same in Twig.

> **Comments:**
> **username_0**: Yes! So simple, so effective! And this would work perfectly with my other scenario where there's different operators for different category groups as well. Thanks.  

---

### Answer by username_3 on 2020-07-02. Score: 1

Not able to comment, so trying an answer.
Did you tried to build a custom hierarchy by scrolling the content of each level in a custom object ?

> **Comments:**
> **username_0**: Hi. Not sure what you mean. Could you explain a bit more? What kind of custom object and custom hierarchy?  

---

