1

Trying to pull in a limit of 2 events that are upcoming (so the event's eventDate should be newer than today). I currently have this:

{% set entries = craft.entries.section('events').order('eventDate asc') %}
{% set today = now|date('Y-m-d') %} 

{% for event in entries %}
  {% if today <= event.eventDate %}
    <!-- markup here -->
  {% endif %}
{% endfor %}

However, this returns all events that are upcoming. I would like to only limit it to 2. The problem when I put the .limit(2) on my for loop is that it chooses the first two (and those happen to be older events). Do I need to do another loop within the if statement and add .limit(2) to that or is there a better way?

Thanks!

1 Answer 1

5

This should work below. Rather than querying all entries and looping through, just select the entries you need (ie. now) and limit them.

{% set upcomingEvents = craft.entries.section('events').eventDate('>= '~now).limit(2) %}
{% for event in upcomingEvents %}
    <!-- markup here -->
{% endfor %} 
4
  • Hi, thanks. Just tried this but its giving me a "Fatal error: Call to a member function getTimestamp() on boolean" with a large call stack table.
    – hidace
    Aug 5, 2016 at 19:23
  • Should also clarify that the date I need to make sure it's displaying after today is the eventDate of the entry (a custom field I set) and not the published or posted date.
    – hidace
    Aug 5, 2016 at 19:26
  • 1
    I guess .after('>= '~now) should be .eventDate('>= '~now), otherwise this looks solid. Aug 5, 2016 at 19:53
  • Yes!! Thanks! Using ".eventDate(...)" worked for me. Appreciate the help!!
    – hidace
    Aug 5, 2016 at 21:29

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.