Clear a transient when new post published

Let’s say you have a recent posts listing, and you’re storing the list of recent posts in a transient to reduce queries. This code will delete the transient when you publish a post so that it’s regenerated using more recent data next time it is requested.

<?php
function be_purge_recent_post_cache( $new, $old, $post ) {
if( 'publish' == $new )
delete_transient( 'recent_posts_query_results' );
}
add_action( ‘transition_post_status’, ‘be_purge_recent_post_cache’, 10, 3 );
view raw functions.php hosted with ❤ by GitHub

Bill Erickson

Bill Erickson is a freelance WordPress developer and a contributing developer to the Genesis framework. For the past 14 years he has worked with attorneys, publishers, corporations, and non-profits, building custom websites tailored to their needs and goals.

Ready to upgrade your website?

I build custom WordPress websites that look great and are easy to manage.

Let's Talk