Purge Related Pages From LiteSpeed Cache on Update
When a post changes, also purges the homepage and the post's category and tag archives from LiteSpeed Cache, so lists and teasers are never out of date.
Opens WPIDE on
Code
add_action( 'transition_post_status', function ( $new, $old, $post ) {
if ( ! defined( 'LSCWP_V' ) || ( 'publish' !== $new && 'publish' !== $old ) || wp_is_post_revision( $post ) || ! is_post_type_viewable( $post->post_type ) ) {
return;
}
do_action( 'litespeed_purge_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 ) ) {
do_action( 'litespeed_purge_url', $link );
}
}
}
}, 20, 3 );
Notes
Uses LiteSpeed Cache's documented purge actions and does nothing if the plugin is inactive.
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.