Last active
April 8, 2022 19:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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