<p>Craft has changed since the above answer was given. The now object must be a part of the string, rather than concatenated as an object.</p>
<p>so instead of:</p>
<p><code>{% set futureEntries = craft.entries.section('events').dateAndTime('&gt;= ' ~ now) %} </code></p>
<p>use:</p>
<p><code>{% set futureEntries = craft.entries.section('events').dateAndTime('&gt;= now') %} </code></p>

CC BY-SA 4.0
2021-10-06T07:30:54.993Z
2021-10-06T07:40:41.597Z
2021-10-06T07:40:41.597Z
ToddPadwick
<p>You can combine a <code>for</code> and <code>if</code> statement in twig, so you don't necessarily need to use <code>search</code>.  </p>

<p>Something like this might get you a bit closer:</p>

<pre><code>{% for entry in craft.entries.section('event') if entry.postDate &lt; now %}
{# output your event here #}
{% endfor %}
</code></pre>

<p>It'll just output what meets the criteria if your <code>if</code> statement. The bonus is it doesn't mess with your loop variables either, so things like <code>loop.index</code> work out as you'd expect (or at least they have for me so far). </p>

<p>EDIT: Here's an alternate way of doing it, which neatens up the query and adds ordering... </p>

<pre><code>{% set events = craft.entries({
    section:  'event',
    startDate:  '&lt; ' ~ now,
    order: 'postDate desc'
}).find() %}

{% for event in events %}
...
{% endfor %}
</code></pre>

CC BY-SA 3.0
2016-08-01T09:06:03.463Z
Not messing with the loop.index is all I needed to tweak it a bit myself thanks!
Justin Dekkers
CC BY-SA 3.0
2016-08-02T15:58:04.063Z
Does this still query the db for all entries within `events`? I'm sure the overhead is minimal but I've achieved the same using `craft.entries.section('event').startDate( '< ' ~ now )` which would only retrieve entries that matched the criteria to then loop through.
Luke Pearce
CC BY-SA 3.0
2016-08-03T07:49:44.930Z
@Luke - I guess it does, but I've not really experienced any performance issues doing it this way, so I've never needed to look into an alternative. If it was to become an issue in future, I'll check out your suggestion, thanks :)
Steve Holland
CC BY-SA 4.0
2018-09-10T16:06:25.360Z
If you were going to add an ORDER BY statement to this, where would it go?
David Rhoden
CC BY-SA 4.0
2018-09-10T16:16:28.940Z
@DavidRhoden I haven't tried it, but you could bolt it onto the `in` part, so `craft.entries.section('event').order('postDate desc')` swap `postDate` for whatever field you want to order it by. However, I'd probably pull that statement out of the for loop now and assign it to a variable first, as it'd be starting to get a bit long and harder to read.
Steve Holland
CC BY-SA 4.0
2018-09-10T16:34:11.903Z
@DavidRhoden I've updated the answer to give an example.
Steve Holland
CC BY-SA 4.0
2021-10-06T07:31:38.370Z
This doesnt seem to work with a dateTime field that contains time as well as date. I get the following error: `Object of class DateTime could not be converted to string`
My query is:
`{% set futureEntries = craft.entries.section('webinars').dateAndTime('> ' ~ now).orderBy('postDate desc') %}`
ToddPadwick
CC BY-SA 4.0
2016-08-01T08:19:33.137Z
2018-09-10T16:31:13.433Z
2018-09-10T16:31:13.433Z
Steve Holland
<p>I'm trying to make a upcoming and past event page. I can make an if statement for displaying the past or upcoming events but the problem is if I limit the amount of entries to be displayed I realised it is still going through the other entries (obviously) so it wont display anything. So I was looking for any filter of some kind to only display entries based on field value greater then other value.</p>

<p>Something in the range of:</p>

<pre><code>{% for entry in craft.entries.section('event').limit(2).search('eventDate &lt; now') %}
&lt;!-- Past events go here --&gt;
{% endfor %}
</code></pre>

<templating><search><datetime><filter><forloop>
Display entries if Field Date is past Now
# Display entries if Field Date is past Now

(Asked by: username_0 on 2016-08-01)

I'm trying to make a upcoming and past event page. I can make an if statement for displaying the past or upcoming events but the problem is if I limit the amount of entries to be displayed I realised it is still going through the other entries (obviously) so it wont display anything. So I was looking for any filter of some kind to only display entries based on field value greater then other value.
Something in the range of:
{% for entry in craft.entries.section('event').limit(2).search('eventDate < now') %}

{% endfor %}

**Tags:** templating, search, datetime, filter, forloop


---

## 2 answers

### Answer by username_2 on 2016-08-01. Score: 11 | Accepted answer

You can combine a for and if statement in twig, so you don't necessarily need to use search.  
Something like this might get you a bit closer:
{% for entry in craft.entries.section('event') if entry.postDate < now %}
{# output your event here #}
{% endfor %}

It'll just output what meets the criteria if your if statement. The bonus is it doesn't mess with your loop variables either, so things like loop.index work out as you'd expect (or at least they have for me so far). 
EDIT: Here's an alternate way of doing it, which neatens up the query and adds ordering... 
{% set events = craft.entries({
    section:  'event',
    startDate:  '< ' ~ now,
    order: 'postDate desc'
}).find() %}

{% for event in events %}
...
{% endfor %}

> **Comments:**
> **username_0**: Not messing with the loop.index is all I needed to tweak it a bit myself thanks!  
> **username_3**: Does this still query the db for all entries within `events`? I'm sure the overhead is minimal but I've achieved the same using `craft.entries.section('event').startDate( '< ' ~ now )` which would only retrieve entries that matched the criteria to then loop through.  
> **username_2**: @Luke - I guess it does, but I've not really experienced any performance issues doing it this way, so I've never needed to look into an alternative. If it was to become an issue in future, I'll check out your suggestion, thanks :)  
> **username_4**: If you were going to add an ORDER BY statement to this, where would it go?  
> **username_2**: @DavidRhoden I haven't tried it, but you could bolt it onto the `in` part, so `craft.entries.section('event').order('postDate desc')` swap `postDate` for whatever field you want to order it by. However, I'd probably pull that statement out of the for loop now and assign it to a variable first, as it'd be starting to get a bit long and harder to read.  
> **username_2**: @DavidRhoden I've updated the answer to give an example.  
> **username_1**: This doesnt seem to work with a dateTime field that contains time as well as date. I get the following error: `Object of class DateTime could not be converted to string` My query is: `{% set futureEntries = craft.entries.section('webinars').dateAndTime('> ' ~ now).orderBy('postDate desc') %}`  

---

### Answer by username_1 on 2021-10-06. Score: 3

Craft has changed since the above answer was given. The now object must be a part of the string, rather than concatenated as an object.

so instead of:

`{% set futureEntries = craft.entries.section('events').dateAndTime('>= ' ~ now) %}`

use:

`{% set futureEntries = craft.entries.section('events').dateAndTime('>= now') %}`


---

