Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save glueckpress/aa10e9c24b596c88d8dbd9a1bff05cf0 to your computer and use it in GitHub Desktop.
Save glueckpress/aa10e9c24b596c88d8dbd9a1bff05cf0 to your computer and use it in GitHub Desktop.
[WordPress][WP Rocket]Clear the cache only for certain pages when a new post of a certain custom post type is published, deleted or changed
<?php
/**
* UNTESTED EXAMPLE!
* Please don’t use in production without having tested it before!
* @param integer $post_id Current post ID
* @return integer Current post ID
*/
function example__cpt_clean_posts_cache( $post_id ) {
$post = get_post( $post_id );
// Check for post type.
if ( 'your_post_type' !== $post->post_type )
return $post_id;
// Static example here, in reality would be retrieved somehow.
$purge_ids = array( '1', '2', '3456' );
// Iterate over given post IDs to clear from cache.
foreach ( $purge_ids as $id )
rocket_clean_post( $id );
return $post_id;
}
add_action( 'wp_trash_post', 'example__cpt_clean_posts_cache' );
add_action( 'delete_post', 'example__cpt_clean_posts_cache' );
add_action( 'clean_post_cache', 'example__cpt_clean_posts_cache' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment