Add custom field to EntryQuery #15721
-
In Craft 4 I used query with custom fields
How I can describe the custom fields in the where condition in Craft 5? |
Beta Was this translation helpful? Give feedback.
Answered by
brandonkelly
Sep 14, 2024
Replies: 1 comment 1 reply
-
You can use the fields’ // Get the “Event” entry type's field layout
$entryType = Craft::$app->entries->getEntryTypeByHandle('event');
$fieldLayout = $entryType->getFieldLayout();
// Get the fields we care about
$publishDateField = $fieldLayout->getFieldByHandle('publishDate');
$endDateField = $fieldLayout->getFieldByHandle('endDate');
$startDateField = $fieldLayout->getFieldByHandle('startDate');
$where = [
'and',
['<=', $publishDateField->getValueSql(), $currentDate],
[
'or',
[
'and',
[$endDateField->getValueSql() => null],
['>=', $startDateField->getValueSql(), $yesterdayDate],
],
['>=', $endDateField->getValueSql(), $yesterdayDate]
]
]; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
brandonkelly
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use the fields’
getValueSql()
methods to get a SQL fragment that can be used in place of the column names: