I am trying to get the entries based on multiple fields. Fields can be of type tags, checkbox, radio box, and number.
In my controller currently what I tried is.
$extrafilter
will be like
Array
(
[0] => Array // other field
(
[handle] => amenities
[value] => laundry
)
[1] => Array // tag field
(
[handle] => technologies
[value] => 45
)
)
Code to narrow down the result.
use Craft;
use craft\elements\Entry;
use craft\elements\db\ElementQuery;
$entryQuery = Entry::find();
$entryQuery->section($sectionName);
if ($extrafilter && !empty($extrafilter[0])) {
$relatedTo = [];
$fieldList = [];
foreach ($extrafilter as $key => $value) {
$relatedTo[] = $value['value'];
$fieldList[] = $value['handle'];
}
$entryQuery->relatedTo = array(
'targetElement' => array_merge(['and'], $relatedTo),
'field' => $fieldList,
);
}
$entries = $entryQuery->all();
This works for Tags but for other fields it dont give any result.
I checked these Questions