<p>Easy! Get the most soonest upcoming pending entry, and use <code>until</code>:</p>

<pre><code>{% 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 %}
</code></pre>

<p>Note that this solution requires <a href="http://buildwithcraft.com/updates#build2598" rel="noreferrer">Craft 2.2.2598</a> or later.</p>

CC BY-SA 3.0
2014-09-18T16:12:33.333Z
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
CC BY-SA 3.0
2014-09-18T16:13:22.313Z
`if craft.config.enableCache` is just a custom config var I'm using, doesn't have anything to do with the question.
Tim Kelty
CC BY-SA 3.0
2014-09-18T16:29:45.077Z
Just FYI... It's not necessary to specify "limit: 1" when you're using `.first`.
Lindsey D
CC BY-SA 3.0
2014-09-18T18:23:28.260Z
Changed my code to reflect that!
Tim Kelty
CC BY-SA 3.0
2014-10-22T21:39:01.847Z
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
CC BY-SA 3.0
2014-10-23T13:32:40.817Z
Awesome, looking forward to it. Thanks, B!
Tim Kelty
CC BY-SA 3.0
2014-10-28T09:50:51.200Z
@TimKelty As of [Craft 2.2.2598](http://buildwithcraft.com/updates#build2598) your original solution should work. Give it a shot!
Brandon Kelly
CC BY-SA 3.0
2014-09-18T16:11:28.400Z
2016-05-02T08:26:39.390Z
2016-05-02T08:26:39.390Z
Tim Kelty
<p>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.</p>

<postdate><pending>
How can I cache a list of entries with pending postDates and have it automatically invalidate the cache appropriately?
# How can I cache a list of entries with pending postDates and have it automatically invalidate the cache appropriately?

(Asked by: username_0 on 2014-09-18)

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.

**Tags:** postdate, pending


---

## 1 answers

### Answer by username_0 on 2014-09-18. Score: 18 | Accepted answer

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.

> **Comments:**
> **username_0**: 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')`  
> **username_0**: `if craft.config.enableCache` is just a custom config var I'm using, doesn't have anything to do with the question.  
> **username_1**: Just FYI... It's not necessary to specify "limit: 1" when you're using `.first`.  
> **username_0**: Changed my code to reflect that!  
> **username_2**: Your original solution will work with a small tweak to the way cache clearing works in Craft. The next update will include that tweak.  
> **username_0**: Awesome, looking forward to it. Thanks, B!  
> **username_2**: @TimKelty As of [Craft 2.2.2598](http://buildwithcraft.com/updates#build2598) your original solution should work. Give it a shot!  

---

