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

The pseudo query would be...

SELECT * FROM `entries`
 (`food_type` = "cake" or `category` = "burger")
(`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..


  • Here's how to do it in Twig: craftcms.stackexchange.com/a/36309/271 - You can probably take that logic and convert it fairly easily into PHP. Dec 18, 2020 at 9:10
  • 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
    Dec 18, 2020 at 11:25

1 Answer 1


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

    ['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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.