2

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

1 Answer 1

2

try:

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

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.