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.

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 the co-founder and lead developer at CultivateWP, a WordPress agency focusing on high performance sites for web publishers.

About Me
Ready to upgrade your website?

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

Let's Talk