1

I want to output all entries which match with a given year from a custom date field. The year is a string I get from slug. I am using Craft 3.5.16. What did I do wrong?

{% set catYear = craft.app.request.segment(-1) %}

{% set firstDayOfYear = now | date_modify('1 January ' ~ catYear) %}
{% set lastDayOfYear = now | date_modify('31 December ' ~ catYear) %}

{% set params = {
  section : 'termine',
  datumTermin : 'and, >= ' ~ firstDayOfYear|date ~ ', <= ' ~ lastDayOfYear|date
} %}

{% set beitrage = craft.entries(params).all %}
3

You need to query using strings and your condition should be written as follow:

{% set catYear = craft.app.request.segment(-1) %}

{% set firstDayOfYear = catYear ~ "-01-01" %}
{% set lastDayOfYear = catYear ~ "-12-31" %}


{% set params = {
    section : 'termine',
    datumTermin : ['and', '>=' ~ firstDayOfYear, '<= ' ~ lastDayOfYear]
} %}

{% set beitrage = craft.entries(params).all %}

See documentation for reference :)

2
  • I tested your code in my environment. It works.
    – thomas
    Dec 4 '20 at 23:03
  • @thomas I deleted your answer since it wasn’t an answer and converted it to a comment here. If Oli’s answer solved the problem for you, please consider accepting it.
    – Matt Stein
    Dec 8 '20 at 16:14
0

Bonus Answer: You can use after and before for that as well like below code.

{% set catYear = craft.app.request.segment(-1) %}

{% set firstDayOfYear = catYear ~ "-01-01" %}
{% set firstDayOfNextYear = firstDayOfYear | date_modify('+1 year') %}
{% set beitrage = craft.entries.after(firstDayOfYear|atom).before(firstDayOfNextYear|atom).all %}

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.