1

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.

Thanks

3

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' %}
<ul>
    {% for entry in craft.entries.section('evenements').order('date desc').date(dateParam) %}
        <li><a href="{{ entry.url }}">{{ entry.title }}</a></li>
    {% endfor %}
</ul>
5
  • 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)] %} – Michael Rog Mar 9 '16 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. – Eric Chantigny Mar 10 '16 at 14:52
  • Cool. Where did you find the example/inspiration for feeding a string into the date param? – Michael Rog Mar 10 '16 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! – Michael Rog Mar 10 '16 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. – Eric Chantigny Mar 12 '16 at 0:30

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.