I am trying to filter entries by a date range using a custom field and finding it more difficult than I think it should be.

I have found the following two questions that seem similar:

How to display entries with a custom date field within month

Filter Entries by Field Value

Both of these seem to use custom methods/object keys that are the name of the field (.eventDate and applicationsClose: in the above questions). I can see this in the documentation, where it says "Most custom fields support element query parameters as well, named after the field handles.".

My custom field is also called eventDate as-per the first question, so I have tried something similar:

{% set entriesQuery = craft
    .eventDate('and', '>= ' ~ '2019-08-01', '<= ' ~ '2019-08-31')
    .orderBy('resourcePinned desc, dateCreated desc')
    .limit(12) %}

(category and search are set elsewhere)

However, when I run this, i get this error:

Calling unknown method: craft\behaviors\ElementQueryBehavior::eventDate()

What am I missing, and what is the correct way to search a date field for a date range?

Edit: So I also have a field called eventAddress, and when I add .eventAddress('*address*') to the query, this works, however doing .eventDate throws the error that the method doesn't exist.

Your Answer

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

Browse other questions tagged or ask your own question.