10

I have a listing page of entries, some of which may have future-dated postDates. I want to cache the output, but only until one of the pending entries goes live.

1 Answer 1

18

Easy! Get the most soonest upcoming pending entry, and use until:

{% set firstPending = craft.entries({
  section: 'articles',
  status: 'pending',
  order: 'postDate asc'
}).first %}
{% set cacheUntil = firstPending ? firstPending.postDate : now|date_modify('+1 year') %}

{% set content %}
    {% cache until cacheUntil %}
        ...
    {% endcache %}
{% endset %}

Note that this solution requires Craft 2.2.2598 or later.

7
  • Not sure if there's a way to include the until statement conditionally all-together, that way you wouldn't need to do now|date_modify('+1 year')
    – Tim Kelty
    Sep 18, 2014 at 16:12
  • if craft.config.enableCache is just a custom config var I'm using, doesn't have anything to do with the question.
    – Tim Kelty
    Sep 18, 2014 at 16:13
  • Just FYI... It's not necessary to specify "limit: 1" when you're using .first.
    – Lindsey D
    Sep 18, 2014 at 16:29
  • Changed my code to reflect that!
    – Tim Kelty
    Sep 18, 2014 at 18:23
  • Your original solution will work with a small tweak to the way cache clearing works in Craft. The next update will include that tweak. Oct 22, 2014 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.