<p>When you are setting <code>nextEvent</code>, you can limit only events that are upcoming and only output the first result you get back like so:</p>

<pre><code>.startDate('&gt;= ' ~ now).first()
</code></pre>

<p>So you can take that and update your code like so:</p>

<pre><code>{% set nextEvent = craft.entries.section('events').order('startDate').startDate('&gt;= ' ~ now).first() %}

{% if nextEvent %}
    foo
{% endif %}
</code></pre>

<p>I have assumed you have a section called 'Event's until we get some clarification as to what <code>craft.events.events</code> is.</p>

CC BY-SA 3.0
2015-05-06T12:27:34.053Z
That was fast enough, Jamie! Congrats. ;) I'd suggest to use `first` method instead of setting a limit, though.
carlcs
CC BY-SA 3.0
2015-05-06T12:35:22.553Z
The code won't work as I'm using the Craft events plugin. {% set nextEvent = craft.entries.events.order('startDate').startDate('>= ' ~ now).limit(1) %} is trowing in an SQL state error (column not found)
Robbie Post
CC BY-SA 3.0
2015-05-06T13:42:53.943Z
@RobbiePost Hi Robbie, please have a look at the answer posted by carlcs. The code I supplied will work, but you will need to make some adjustments to how you handle events, as that plugin shouldn't be used in production sites.
Jamie Wade
CC BY-SA 3.0
2015-05-06T12:22:40.180Z
2015-05-06T12:34:32.827Z
2015-05-06T12:34:32.827Z
Jamie Wade
<p>P&amp;T's events plugin available on GitHub <a href="http://github.com/pixelandtonic/Events" rel="nofollow">http://github.com/pixelandtonic/Events</a> is actually not meant to be used at all. It was just a demo to show the potential of custom element types!</p>

<p>I'd suggest to use a channel section for your events, and with the code Jamie provided you're already done figuring out how to find your "next event"! You could also collect events with a matrix field, if you have a need for an extra level of hierarchy and if that's still not enough you could have your events in a table in that matrix (using the <a href="https://github.com/engram-design/SuperTable" rel="nofollow">"Super Table"</a> plugin).</p>

<p>There's also two other plugins: <a href="https://github.com/tippingmedia/craft.events" rel="nofollow">"Events"</a> and <a href="http://michaelrog.com/craft/calendars/introduction" rel="nofollow">"Calendar"</a>, but I'm not sure if they are still under development and I'd probably stick to core Craft features (channels / matrix) for now.</p>

CC BY-SA 3.0
2015-05-06T13:41:32.837Z
Thanks for clarifying carlcs. I agree, the P&T plugin is a no-go and a channel would be the best option for this.
Jamie Wade
CC BY-SA 3.0
2015-05-06T14:13:42.060Z
For me events plugin was a quick and simple solution for a simple project. I solved the issue by hiding the generated list in HTML and only showing the first-child. Thanks for the suggestions
Robbie Post
CC BY-SA 3.0
2015-05-06T14:28:12.023Z
There's a better way to do this, @Robbie. I just posted another answer, but please do me a favor and don't accept it. Using P&T plugin is indeed a no-go ...
carlcs
CC BY-SA 3.0
2015-05-07T06:20:48.833Z
The breakloop suggestion did the job and solved the issue. It's clear now that the P&T events plugin is not the way to go
Robbie Post
CC BY-SA 3.0
2015-05-06T13:15:14.970Z
2015-05-06T13:15:14.970Z
carlcs
<p>To break a for loop you could have a variable and check against its value in your loop:</p>

<pre><code>{% set breakLoop = false %}

{% for entry in nextEvent %}

    {% if (entry.startDate &gt; now) and (breakLoop == false) %}
        {{ entry.startDate }}
        {% set breakLoop = true %}
    {% endif %}

{% endfor %}
</code></pre>

<p>Or – if you have a need to break loops more often – use the plugin <a href="https://github.com/marionnewlevant/craft-mnbreakandcontinue" rel="nofollow">"Break and Continue"</a>, that gives you a Twig tag for this:</p>

<pre><code>{% for entry in nextEvent %}

    {% if entry.startDate &gt; now %}
        {{ entry.startDate }}
        {% break %}
    {% endif %}

{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-05-06T14:25:12.720Z
2015-05-06T14:25:12.720Z
carlcs
<p>I'm trying to find the next event after today (now) from a series of entries. This code however outputs all upcoming events after today but I'm just looking for the first (next) event.</p>

<p>Any help is appreciated</p>

<pre><code>{% set nextEvent =  craft.events.events.find() %}
{% for entry in nextEvent %}
    {% if entry.startDate &gt; now %}
    &lt;span class="text-primary"&gt;{{ entry.title }}&lt;/span&gt;&lt;br&gt;
    {{ entry.startDate|date('d F Y ') }}
    {% endif %}
{% endfor %}
</code></pre>

CC BY-SA 3.0
2015-05-06T12:17:58.273Z
Is this a plugin you're using or is `craft.events.events.find()` a typo in your question?
carlcs
CC BY-SA 3.0
2015-05-06T12:30:49.863Z
The find() is a typo. I'm using the Craft events plugin for this.
Robbie Post
<entries><datetime><events>
Find next event
# Find next event

(Asked by: username_0 on 2015-05-06)

I'm trying to find the next event after today (now) from a series of entries. This code however outputs all upcoming events after today but I'm just looking for the first (next) event.
Any help is appreciated
{% set nextEvent =  craft.events.events.find() %}
{% for entry in nextEvent %}
    {% if entry.startDate > now %}
    {{ entry.title }}
    {{ entry.startDate|date('d F Y ') }}
    {% endif %}
{% endfor %}

**Tags:** entries, datetime, events
> **Comments:**
> **username_1**: Is this a plugin you're using or is `craft.events.events.find()` a typo in your question?  
> **username_0**: The find() is a typo. I'm using the Craft events plugin for this.  

---

## 3 answers

### Answer by username_2 on 2015-05-06. Score: 3

When you are setting nextEvent, you can limit only events that are upcoming and only output the first result you get back like so:
.startDate('>= ' ~ now).first()

So you can take that and update your code like so:
{% set nextEvent = craft.entries.section('events').order('startDate').startDate('>= ' ~ now).first() %}

{% if nextEvent %}
    foo
{% endif %}

I have assumed you have a section called 'Event's until we get some clarification as to what craft.events.events is.

> **Comments:**
> **username_1**: That was fast enough, Jamie! Congrats. ;) I'd suggest to use `first` method instead of setting a limit, though.  
> **username_0**: The code won't work as I'm using the Craft events plugin. {% set nextEvent = craft.entries.events.order('startDate').startDate('>= ' ~ now).limit(1) %} is trowing in an SQL state error (column not found)  
> **username_2**: @RobbiePost Hi Robbie, please have a look at the answer posted by carlcs. The code I supplied will work, but you will need to make some adjustments to how you handle events, as that plugin shouldn't be used in production sites.  

---

### Answer by username_1 on 2015-05-06. Score: 2

P&T's events plugin available on GitHub <http://github.com/pixelandtonic/Events> is actually not meant to be used at all. It was just a demo to show the potential of custom element types!

I'd suggest to use a channel section for your events, and with the code Jamie provided you're already done figuring out how to find your "next event"! You could also collect events with a matrix field, if you have a need for an extra level of hierarchy and if that's still not enough you could have your events in a table in that matrix (using the ["Super Table"](https://github.com/engram-design/SuperTable) plugin).

There's also two other plugins: ["Events"](https://github.com/tippingmedia/craft.events) and ["Calendar"](http://michaelrog.com/craft/calendars/introduction), but I'm not sure if they are still under development and I'd probably stick to core Craft features (channels / matrix) for now.

> **Comments:**
> **username_2**: Thanks for clarifying carlcs. I agree, the P&T plugin is a no-go and a channel would be the best option for this.  
> **username_0**: For me events plugin was a quick and simple solution for a simple project. I solved the issue by hiding the generated list in HTML and only showing the first-child. Thanks for the suggestions  
> **username_1**: There's a better way to do this, @Robbie. I just posted another answer, but please do me a favor and don't accept it. Using P&T plugin is indeed a no-go ...  
> **username_0**: The breakloop suggestion did the job and solved the issue. It's clear now that the P&T events plugin is not the way to go  

---

### Answer by username_1 on 2015-05-06. Score: 2

To break a for loop you could have a variable and check against its value in your loop:
{% set breakLoop = false %}

{% for entry in nextEvent %}

    {% if (entry.startDate > now) and (breakLoop == false) %}
        {{ entry.startDate }}
        {% set breakLoop = true %}
    {% endif %}

{% endfor %}

Or – if you have a need to break loops more often – use the plugin "Break and Continue", that gives you a Twig tag for this:
{% for entry in nextEvent %}

    {% if entry.startDate > now %}
        {{ entry.startDate }}
        {% break %}
    {% endif %}

{% endfor %}


---

