<p>I don't think you're going to be able to do what you're looking for purely through Twig.</p>

<p>You can, however, write a plugin that either uses a <a href="http://buildwithcraft.com/docs/plugins/services" rel="nofollow">service</a> and <a href="http://buildwithcraft.com/docs/plugins/variables" rel="nofollow">variable</a> or a <a href="http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail" rel="nofollow">Template Hook</a>, so you can drop down to the PHP level and use <a href="http://buildwithcraft.com/classreference/etc/db/DbCommand" rel="nofollow">DbCommand's</a> <a href="http://www.yiiframework.com/doc/api/1.1/CDbCommand#where-detail" rel="nofollow">where</a> to apply your extra date conditionals to your SQL query directly.</p>

CC BY-SA 3.0
2015-04-23T19:56:07.627Z
2015-04-23T19:56:07.627Z
Brad Bell
<p>I have a set of parameters that need to be checked before outputting an entry in a particular section of a website. The code for that looks somewhat like this:</p>

<pre><code>{% for slide in craft.entries.section('events').featured('1').order('dateTime asc').limit(2) if (slide.dateTime|date_modify("tomorrow 5am")|date("c")) &gt;= now|date("c") %}
</code></pre>

<p>The problem is that it obviously is looping through all entries, even those that do not match my conditional based on the date. Is there a way to prevent the loop through entries that don't match that condition to prevent bloat as there will eventually be thousands of entries that it would be looped through that do not end up being output due to the conditional.</p>

CC BY-SA 3.0
2015-04-23T19:06:47.760Z
If your conditional doesn't seem to be getting applied, then I'd guess that it's not doing what you think it's doing.  Try breaking it up into smaller steps (grab the all entries in one statement, then use the if statement inside of a for loop) just to make sure everything is kosher.
Brad Bell
CC BY-SA 3.0
2015-04-23T19:12:13.750Z
Brad, the conditional is doing what it should which is outputting only entries that match it's conditions. My question was is there a way to limit craft from even looping through those entries (output or not) so as to avoid having to loop through thousands of entries that don't meet the conditional's requirements and bloating the query as more entries are added over time.
Christopher Healey
CC BY-SA 3.0
2015-04-23T19:26:44.383Z
Ahh, sorry... misunderstood.
Brad Bell
<forloop><queries><params>
Prevent queries based on condition
# Prevent queries based on condition

(Asked by: username_0 on 2015-04-23)

I have a set of parameters that need to be checked before outputting an entry in a particular section of a website. The code for that looks somewhat like this:
{% for slide in craft.entries.section('events').featured('1').order('dateTime asc').limit(2) if (slide.dateTime|date_modify("tomorrow 5am")|date("c")) >= now|date("c") %}

The problem is that it obviously is looping through all entries, even those that do not match my conditional based on the date. Is there a way to prevent the loop through entries that don't match that condition to prevent bloat as there will eventually be thousands of entries that it would be looped through that do not end up being output due to the conditional.

**Tags:** forloop, queries, params

> **Comments:**
> **username_1**: If your conditional doesn't seem to be getting applied, then I'd guess that it's not doing what you think it's doing.  Try breaking it up into smaller steps (grab the all entries in one statement, then use the if statement inside of a for loop) just to make sure everything is kosher.  
> **username_0**: Brad, the conditional is doing what it should which is outputting only entries that match it's conditions. My question was is there a way to limit craft from even looping through those entries (output or not) so as to avoid having to loop through thousands of entries that don't meet the conditional's requirements and bloating the query as more entries are added over time.  
> **username_1**: Ahh, sorry... misunderstood.  

---

## 1 answers

### Answer by username_1 on 2015-04-23. Score: 1

I don't think you're going to be able to do what you're looking for purely through Twig.

You can, however, write a plugin that either uses a [service](http://buildwithcraft.com/docs/plugins/services) and [variable](http://buildwithcraft.com/docs/plugins/variables) or a [Template Hook](http://buildwithcraft.com/classreference/services/TemplatesService#hook-detail), so you can drop down to the PHP level and use [DbCommand's](http://buildwithcraft.com/classreference/etc/db/DbCommand) [where](http://www.yiiframework.com/doc/api/1.1/CDbCommand#where-detail) to apply your extra date conditionals to your SQL query directly.


---

