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.

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.

  • 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 '14 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 '14 at 16:13
  • Just FYI... It's not necessary to specify "limit: 1" when you're using .first. – Lindsey D Sep 18 '14 at 16:29
  • Changed my code to reflect that! – Tim Kelty Sep 18 '14 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. – Brandon Kelly Oct 22 '14 at 21:39

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.