Skip to content

Instantly share code, notes, and snippets.

@petenelson
Created October 11, 2019 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petenelson/f314edc59a422fdf8e940fdd45c03050 to your computer and use it in GitHub Desktop.
Save petenelson/f314edc59a422fdf8e940fdd45c03050 to your computer and use it in GitHub Desktop.
WordPress Cache Incrementor
/**
* Gets or updates the cache incrementor for a post type.
*
* @param string $post_type The post type.
* @param bool $update Whether to force an incrementor update.
* @return string
*/
function post_type_cache_incrementor( $post_type, $update = false ) {
$group = 'post_type_cache_incrementor';
if ( $update ) {
wp_cache_delete( $post_type, $group );
}
$incrementor = wp_cache_get( $post_type, $group );
if ( false === $incrementor ) {
$incrementor = current_time( 'timestamp' );
wp_cache_set( $post_type, $incrementor, $group, HOUR_IN_SECONDS );
}
return $incrementor;
}
/**
* Updates the cache incrementor for a post type.
*
* @param int|WP_Post $post The post ID or object.
* @return void
*/
function update_post_type_cache_incrementor( $post ) {
// Check autosave.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
$post = get_post( $post );
if ( is_a( $post, '\WP_Post' ) ) {
post_type_cache_incrementor( $post->post_type, true );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment