I have a date field (date) in my entry and I wish to get all entries of a specified year. Can’t figure it out at all. Here’s what I have now, to sort them by that field:

craft.entries.section('evenements').order('date desc')

My "year" is passed to the page through routing and is named "year". I can get that.


1 Answer 1


Looks like I finally found an answer to it

year being my param

date being my custom date field

{% set dateParam = 'and, >= ' ~ year ~ '-01-01, < ' ~ (year + 1) ~ '-01-01' %}
    {% for entry in craft.entries.section('evenements').order('date desc').date(dateParam) %}
        <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
    {% endfor %}
  • Curiously, does that work? I would think you'd need to pass an array into that param, like this: {% set dateParam = ['and', '>= ' ~ year, '< ' ~ (year + 1)] %} Commented Mar 9, 2016 at 22:33
  • Well, it does work like I wrote it. But, it works the way you did it too. I get the exact same results both ways. Interesting... Keeping your way in my code though, it is definitely cleaner. Commented Mar 10, 2016 at 14:52
  • Cool. Where did you find the example/inspiration for feeding a string into the date param? Commented Mar 10, 2016 at 15:06
  • p.s. I double-checked with Brad. He confirmed that date parameters accept a string or an array by design: DateFieldType's modifyElementsQuery calls DbHelper::parseDateParam() and the first thing that does is convert the value to an array if it's not one: github.com/pixelandtonic/Craft-Release/blob/master/app/helpers/… — Pretty cool! Commented Mar 10, 2016 at 23:25
  • I found it on another question here somewhere. I can't remember how I found it though. I repurposed the answer to my situation and it worked. Commented Mar 12, 2016 at 0:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.