1

I'm trying to filter entries on the front end by a date range but no matter what I do I end up with no results, despite there being results in the, temporarily hard coded, date range I am using.


  {% set page = page ?? 1 %}
  {# set search parameter here #}
  {% set search = q ?? '' %}
  {# stage #}
  {% set stage = stage ?? '' %}
  {# date #}
  {% set start = now|date_modify('-16 week')|atom %}
  {% set end = now|date_modify('-1 week')|atom %}


  {% set entryQuery = craft.entries
    .section('projects')
    .limit(limit)
    .stage(stage)
    .startDate(['and', '>= #{start}', '< #{end}'])
    .search(search) %}
    
    {% for entry in entries %}
      {% include '_includes/project-listing-card' %}
    {% endfor %}

The code above sets some parameters based on the url and defaults. Without the .startDate line this outputs entries as expected. However once I add in the startDate (a date field) nothing gets output.

How do I output entries that are in the date range?

2
  • 1
    I think you need to use double quotes for string interpolation, so ">= #{start}" and "< #{end}". Commented Jan 14, 2021 at 14:39
  • @SteveRowling that fixed it, if you make this an answer, I'll mark it correct
    – CreateSean
    Commented Jan 14, 2021 at 15:02

1 Answer 1

3

String interpolation in Twig only works with double-quoted strings.

Change the .startDate line to .startDate(['and', ">= #{start}", "< #{end}"]) and it should work.

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.