1

I've a URL /news/archive/2014/12

A route parses that as going to an archive template and captures a year and month variable. I then want to select all the entries from that month, and I can't work out how. Here's what works for just the year:

{% set entriesInPeriod = craft.entries.section('news').limit(null).after(year).before(year+1) %}

And the docs say the after and before functions need things specified in the format:

YYYY
YYYY-MM

I can't get that passed in because I don't know how Craft is interpolating the contents of the parenthasise; if I do

{% set entriesInPeriod = craft.entries.section('news').limit(null).after(year-month).before(year-month+1) %}

I'm not getting any results.

UPDATE: Answer below (use ~ to concat strings) doesn't work correctly because in reality months 'loop' after 12. What gets put in the query when starting in December evaluates to 'month after 12 and before 13', which is garbage.

Is there a way to get something like:

{% set entriesInPeriod = craft.entries.section('news').limit(null).postDate(year~'-'~month) %}

This doesn't work, but I can find no documentation on how postDate should be formatted in this use case.

4

In Twig, you concatenate strings with the ~ operator. So, if year and month are variables in your template, you can create a string in the required format like this

{% set yearMonth = year ~ '-' ~ month %}
  • Perfect, hadn't thought to do an interim variable; thanks. – Matt Wilcox Dec 10 '14 at 14:13
  • 1
    You can do this directly inside after too: craft.entries.section('news').limit(null).after(year~'-'~month).before(year~'-'~(month+1)) – André Elvan Dec 10 '14 at 14:25
2

I've worked around this as follows:

{% if month is not defined %}
    {% set before = year+1 %}
    {% set after  = year %}
{% else %}
    {% if month == 12 %}
        {% set yearBefore = year + 1 %}
        {% set monthBefore = '01' %}

        {% set before = yearBefore~'-'~monthBefore %}
        {% set after = year~'-'~month %}
    {% else %}
        {% set before = year ~ '-' ~ (month+1) %}
        {% set after  = year ~ '-' ~ month %}
    {% endif %}
{% endif %}

{% set entriesInPeriod = craft.entries.section('news').limit(null).after(after).before(before) %}

Which seems to be working well. Thanks all.

  • 2
    I think I was adding an answer at the same time as you. Glad you found a solution. Keep in mind, you may need to zero-pad the month if it is a single digit using "%02d"|format(month + 1). Although I'm not sure — it may accept single digit month values. – Douglas McDonald Dec 10 '14 at 16:30
1

This should solve the month = 13 problem.

{% set startDate = year ~ '-' ~ month %}
{% if month == 12 %}
    {% set endDate = (year + 1) ~ '-01' %}
{% else %}
    {% set endDate = year ~ '-' ~ "%02d"|format(month + 1) %}
{% endif %}

{% set entries = craft.entries.section('news').limit(null).after(startDate).before(endDate) %}

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.