1

I'm attempting to merge in postDate restrictions based on the url segment. However I get twig errors with my code.

So for a link of domain.com/news/archive/2016 I want to see all entries with a postDate in 2016.

current code:

{% set params = { section: 'news', limit: 5 } %}

{# check for annual archive in the url #}
{% if craft.app.request.getSegment(2) == "archive" %}
    {% set year = craft.app.request.getSegment(3) %}

    {% set start = date( year ~ '-01-01') %} 
    {% set end = date( year ~ '-12-31') %}

    {% set params = params | merge({ 
      postDate(['and', ">= " ~ start, "< " ~ end])
     }) %}
{% endif %}
{# end check for annual archive #}

And the Error I get

A hash key must be followed by a colon (:). Unexpected token "punctuation" of value "(" ("punctuation" expected with value ":").

Also not working I tried a couple of variations - this gets no errors, but also no entries are output

{% set params = { section: 'news', limit: 5 } %}

{# check for annual archive in the url #}
{% if craft.app.request.getSegment(2) == "archive" %}
    {% set year = craft.app.request.getSegment(3) %}

    {% set start = date( year ~ '-01-01') %} 
    {% set end = date( year ~ '-12-31') %}

    {% set params = params | merge({ 
      postDate: "['and', '>= ' ~ start, '< ' ~ end]",
     }) %}
{% endif %}
2

try:

{% set params = params | merge({
    postDate:['and', '>= ' ~ start.date, '< ' ~ end.date]
}) %}
  • Perfect. Thank you. – CreateSean May 15 '19 at 13:01

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.