TransWikia.com

Filter Entries by Custom date

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 %}

2 Answers

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 :)

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

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP