1

I have a custom date field - "renewalDate" - which I want to use to limit an entry query. Here is the code:

When I run that it throws this error:

"Calling unknown method: craft\behaviors\ElementQueryBehavior::renewalDate()"

{% set dateStart = now|date('Y-m-d H:i')  %}
{% set dateEnd = date('next month') | date('Y-m-d H:i')  %}

{% set list = craft.users()
.group('members') 
.status(null)
.orderBy('lastName asc')
.renewalDate('and', '>= #{dateStart}', '< #{dateEnd}')

%}

However, this works fine:

{% set list = craft.users()
.group('members') 
.status(null)
.orderBy('lastName asc')
.renewalDate('>= #{dateStart}') 
%}

So, the problem seems to be related to using "and".

I have done this before using Craft 2, but it is not working with Craft 3. I am missing something here?

Thanks!

2

Have you tried

.renewalDate('and', ">= #{dateStart}", "< #{dateEnd}")

The difference is the double quotes. I don't think that single quotes support interpolation.

However, I'm not sure in that case why the version without the 'and' works.

OK, have just tried it myself... I created a section called test and added a single date field called renewalDate and the code below works on a simple listing page.

{% set dateStart = now|date('Y-m-d H:i')  %}
{% set dateEnd = date('next month') | date('Y-m-d H:i')  %}

{% set params = {
  section : 'test',
  orderBy : 'renewalDate asc',
  renewalDate : ['and', ">= #{dateStart}", "< #{dateEnd}"]
} %}

<h1>Entries</h1>
{% for entry in craft.entries(params).all() %}
    <p>{{ entry.title }}</p>
{% endfor %}
  • Tried that but no luck - thanks, though! – Roi Agneta Jan 2 at 19:18
  • OK, have just tried it myself and have edited my answer with some actual working code. It's not exactly the same as yours but it's very close. Interestingly it only worked with double quotes. In my version the 'and' and two filters are in an array, so maybe that is what you need on your code. – JollyR Jan 2 at 23:29
  • Thanks @JollyR - I had tried using an array at one point, but guess I did not use double quote. Also, I checked the docs and there are a few examples that clearly show single quotes around the 'and' with double quotes around the parameters, so guess I should have paid more attention. Thanks again for your persistence! – Roi Agneta Jan 4 at 16:59

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.