Flush Related Pages From W3 Total Cache on Update
When a post changes, also flushes the homepage and the post's category and tag archives from W3 Total Cache's page cache, so lists update with the post.
Opens WPIDE on
Code
add_action( 'transition_post_status', function ( $new, $old, $post ) {
if ( ! function_exists( 'w3tc_flush_url' ) || ( 'publish' !== $new && 'publish' !== $old ) || wp_is_post_revision( $post ) || ! is_post_type_viewable( $post->post_type ) ) {
return;
}
w3tc_flush_url( home_url( '/' ) );
foreach ( get_object_taxonomies( $post->post_type ) as $taxonomy ) {
if ( ! is_taxonomy_viewable( $taxonomy ) ) {
continue;
}
foreach ( (array) wp_get_post_terms( $post->ID, $taxonomy ) as $term ) {
$link = get_term_link( $term );
if ( ! is_wp_error( $link ) ) {
w3tc_flush_url( $link );
}
}
}
}, 20, 3 );
Notes
Uses W3 Total Cache's public flush functions and does nothing if it is not active.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.