clean_post_cache( int|WP_Post $post )
- Since
- 2.0.0
- Source
wp-includes/post.php:7719
Description
Cleaning means delete from the cache of the post. Will call to clean the term object cache associated with the post ID.
This function not run if $_wp_suspend_cache_invalidation is not empty. See wp_suspend_cache_invalidation().
Compatibility
- WordPress
- since 2.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$postint|WP_Post- Post ID or post object to remove from the cache.
Performance profile
How much work a call to clean_post_cache() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Constant
- Instructions
- 5–62
- Plugin surface
- 2 hooks
- Called by
- 14
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 64.
Third-party callbacks on 'clean_post_cache', 'clean_page_cache' run inside this call, and their cost is not bounded by anything here.
14 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- querycontent query
get_post()called directly - cacheobject cache
wp_cache_delete()called directly - hookthird-party callbacks
do_action()called directly
Further down the call graph this can also reach option, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost clean_post_cache() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!empty($_wp_suspend_cache_invalidation) | 5 | none |
empty($_wp_suspend_cache_invalidation) | 10 | get_post() |
empty($_wp_suspend_cache_invalidation) | 52 | get_post(), wp_cache_delete(), wp_cache_delete(), wp_cache_delete(), clean_object_term_cache(), wp_cache_delete(), do_action(), wp_cache_set_posts_last_changed() |
empty($_wp_suspend_cache_invalidation) | 62 | get_post(), wp_cache_delete(), wp_cache_delete(), wp_cache_delete(), clean_object_term_cache(), wp_cache_delete(), do_action(), wp_cache_delete(), do_action(), wp_cache_set_posts_last_changed() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 64 instructions, 5–62 executed per call, 3 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 2
2 hooks fire while clean_post_cache() runs, in this order:
- do_action( clean_post_cache )actionline 7748 (+29 into the body)
Fires immediately after the given post's cache is cleaned.
- do_action( clean_page_cache )actionline 7760 (+41 into the body)
Fires immediately after the given page's cache is cleaned.
Uses · 5
- get_post()Retrieves post data given a post ID or post object.
- wp_cache_delete()Removes the cache contents matching key and group.
- clean_object_term_cache()Removes the taxonomy relationship to terms from the cache.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_cache_set_posts_last_changed()Sets the last changed time for the 'posts' cache group.
Used by · 14
- WP_Customize_Manager::_publish_changeset_values()Publishes the values of a changeset.
- WP_Customize_Manager::trash_changeset_post()Trashes or deletes a changeset post.
- WP_Posts_List_Table::_display_rows_hierarchical()
- _wp_keep_alive_customize_changeset_dependent_auto_drafts()Makes sure that auto-draft posts get their post_date bumped or status changed to draft to prevent premature garbage-collection.
- add_ping()Adds a URL to those already pinged.
- clean_page_cache()Will clean the page in the cache.
- set_post_type()Updates the post type for the post ID.
- wp_add_trashed_suffix_to_post_name_for_post()Adds a trashed suffix for a given post.
- wp_delete_attachment()Trashes or deletes an attachment.
- wp_delete_post()Trashes or deletes a post or page.
- wp_delete_user()Delete user and optionally reassign posts and links to another user.
- wp_insert_post()Inserts or update a post.
Show all 14
- wp_publish_post()Publishes a post by transitioning the post status.
- wp_update_comment_count_now()Updates the comment count for the post.
Source code
function clean_post_cache( $post ) { global $_wp_suspend_cache_invalidation; if ( ! empty( $_wp_suspend_cache_invalidation ) ) { return; } $post = get_post( $post ); if ( ! $post ) { return; } wp_cache_delete( $post->ID, 'posts' ); wp_cache_delete( 'post_parent:' . (string) $post->ID, 'posts' ); wp_cache_delete( $post->ID, 'post_meta' ); clean_object_term_cache( $post->ID, $post->post_type ); wp_cache_delete( 'wp_get_archives', 'general' ); /** * Fires immediately after the given post's cache is cleaned. * * @since 2.5.0 * * @param int $post_id Post ID. * @param WP_Post $post Post object. */ do_action( 'clean_post_cache', $post->ID, $post ); if ( 'page' === $post->post_type ) { wp_cache_delete( 'all_page_ids', 'posts' ); /** * Fires immediately after the given page's cache is cleaned. * * @since 2.5.0 * * @param int $post_id Post ID. */ do_action( 'clean_page_cache', $post->ID ); } wp_cache_set_posts_last_changed();}Changelog
Introduced in 2.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.9.7 tag, from
src/wp-includes/post.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.