Craft CMS Asked on March 6, 2021
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 %}
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 %}
Answered by Oli on March 6, 2021
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 %}
Answered by ZealousWeb on March 6, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP