Skip to content

Instantly share code, notes, and snippets.

@terryupton
Created April 15, 2025 08:08
Show Gist options
  • Save terryupton/cd2c1ffcaff54922e42ac9b53e8400c1 to your computer and use it in GitHub Desktop.
Save terryupton/cd2c1ffcaff54922e42ac9b53e8400c1 to your computer and use it in GitHub Desktop.
How to cache bust in Craft CMS based on pending posts/dates.
{# Fetch the next **pending** entry due to go live #}
{% set nextToCome = craft.entries()
.section('articles')
.status('pending')
.orderBy('postDate asc')
.one() %}
{# If there is no entry due to go live, cache the fragment for a year (change to what you need) #}
{% set cacheExpiry = nextToCome ? nextToCome.postDate : now|date_modify('+1 year') %}
{% cache until cacheExpiry %}
{# Query in your cache tags #}
{% set items = craft.entries
.section('articles')
.orderBy('postDate asc')
.all()
%}
{# Your loop... #}
{% endcache %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment