2

I have two date fields in my entry and I want to return those entries with either a start date OR and end date within a calendar year.

I can get those with a startDate within the calendar year using:

{% set nextyear = year +1 %}
{% set yearbegins = '1 January ' ~ year %}
{% set yearends = '1 January' ~ nextyear %}
{% set unixbegins = yearbegins | date("U") %}
{% set unixends = yearends | date("U") %}

{% set entries = craft.entries.section('events').limit(null).startDate('and', '>= ' ~ unixbegins, '< ' ~ unixends) %}

How to I then run a test on endDate to further filter them down? 

  • How come this code works for you now? I think you had to use the date('U') filter inside the query as you wrote here? – carlcs Jun 23 '14 at 16:38
  • I don't know. I was surprised to find that happen in the other post, too. When I get chance I'll review it, but this is a different template. – Clive Portman Jun 23 '14 at 20:48
  • thanks a lot. your code got me started for my own project. but where does this syntax come from (cf_date('and', '>= ' ~ unixbegins, '< ' ~ unixends, '< ' ~ now))? are there any docs on this? – outline4 Aug 6 '14 at 11:49
2

You could do two queries to get the Ids of the desired entries, merge the Ids and then query the entries with these Ids:

{# Get Ids of desired entries #}
{% set startDateIds = craft.entries.startDate('and', '>= ' ~ unixbegins, '< ' ~ unixends).ids() %}
{% set endDateIds = craft.entries.endDate('and', '>= ' ~ unixbegins, '< ' ~ unixends).ids() %}

{# Merge queried Ids #}
{% set filteredIds = startDateIds|merge(endDateIds) %}

{# Get entries with Id paramenter #}
{% set filteredEntries = craft.entries.id(filteredIds) %}

.

Another idea is to query all potential entries, loop those and filter within the loop. This is probably the way to go if you need to combine your conditionals with other logic then the and operator:

{# Get all potential entries #}
{% set allEntries = craft.entries.section('events').limit(null).find() %}

{# Define array before loop #}
{% set filteredEntries = {} %}

{# Loop all entries #}
{% for key, entry in allEntries %}

    {# Set conditionals to filter for desired entries #}
    {% if ( entry.startDate >= unixbegins and entry.startDate < unixends ) and ( entry.endDate >= unixbegins and entry.endDate < unixends ) %}

        {# Merge entries #}
        {% set filteredEntries = filteredEntries|merge({ (key): entry }) %}
    {% endif %}

{% endfor %}
  • Error: The merge filter only works with arrays or hashes. Both entriesStartDate and entriesEndDate return a llist of entries correctly. – Clive Portman Jun 24 '14 at 5:35
  • Actually, I don't want merge, do I? I only want entries which in both entriesStartDate and entriesEndDate. – Clive Portman Jun 24 '14 at 5:47
  • Hmm, you wrote "with either a start date OR and end date within a calendar year" that means all entries with a start date in that year PLUS all entries with an end date in it. – carlcs Jun 24 '14 at 8:42
  • Yeah, you're right. I do need merge. As long as they aren't repeated. – Clive Portman Jun 24 '14 at 10:27
  • Yep, the new improved merge solved it. Thanks. – Clive Portman Jun 24 '14 at 10:45

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.