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? 

carlcs
  • 36,220
  • 5
  • 62
  • 139
Clive Portman
  • 2,908
  • 17
  • 36
  • 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 06 '14 at 11:49

1 Answers1

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 %}
carlcs
  • 36,220
  • 5
  • 62
  • 139
  • 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 05: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 05: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 08: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
  • That's good to hear, @Clive. I'll delete that old code for clarity. – carlcs Jun 24 '14 at 10:59
  • Just so you know, there's something in lines 2 and 3 of your first answer which knackers a Bootstrap column for Webkit browsers. I don't know quite where is was, but typing them out rather than pasting them fixed it. – Clive Portman Jun 25 '14 at 19:11