1

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.

3
  • 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
    Apr 23 '15 at 19:06
  • 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. Apr 23 '15 at 19:12
  • Ahh, sorry... misunderstood.
    – Brad Bell
    Apr 23 '15 at 19:26
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 and variable or a Template Hook, so you can drop down to the PHP level and use DbCommand's where to apply your extra date conditionals to your SQL query directly.

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.