diff --git a/src/wp-admin/includes/user.php b/src/wp-admin/includes/user.php index abed2d20157c5..1d6f73ea8c8db 100644 --- a/src/wp-admin/includes/user.php +++ b/src/wp-admin/includes/user.php @@ -445,6 +445,10 @@ function wp_delete_user( $id, $reassign = null ) { clean_user_cache( $user ); + foreach( get_post_types() as $post_type ) { + clear_user_posts_count_cache( $user->ID, $post_type ); + } + /** * Fires immediately after a user is deleted from the site. * diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index f006a16a4704c..bce9637c6191b 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -117,8 +117,8 @@ } // User post counts. +add_action( 'attachment_updated', '_clear_user_posts_count_cache_on_author_change', 10, 3 ); add_action( 'post_updated', '_clear_user_posts_count_cache_on_author_change', 10, 3 ); -add_action( 'save_post', '_clear_user_posts_count_cache_on_update', 10, 2 ); // Post meta. add_action( 'added_post_meta', 'wp_cache_set_posts_last_changed' ); diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index eb90e762f5663..c945abe8c43e9 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -3819,6 +3819,8 @@ function wp_delete_post( $post_id = 0, $force_delete = false ) { } } + clear_user_posts_count_cache( $post->post_author, $post->post_type ); + wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); /** @@ -5070,6 +5072,8 @@ function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true ) */ do_action( 'wp_insert_post', $post_id, $post, $update ); + clear_user_posts_count_cache( $post->post_author, $post_type ); + if ( $fire_after_hooks ) { wp_after_insert_post( $post, $update, $post_before ); } @@ -5236,6 +5240,8 @@ function wp_publish_post( $post ) { /** This action is documented in wp-includes/post.php */ do_action( 'wp_insert_post', $post->ID, $post, true ); + clear_user_posts_count_cache( $post->post_author, $post->post_type ); + wp_after_insert_post( $post, true, $post_before ); } diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index f8411fbc1a9b7..8c782975c3f14 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -635,27 +635,6 @@ function _clear_user_posts_count_cache_on_author_change( $post_id, $post_after, } } -/** - * When the post is created, clear the cache. - * - * @since 6.8.0 - * @access private - * - * @param int $post_id Post ID. - * @param WP_Post $post Post object. - */ -function _clear_user_posts_count_cache_on_update( $post_id, $post ) { - // Don't do anything if revision is being saved. - if ( wp_is_post_revision( $post_id ) ) { - return; - } - - $post_type = $post->post_type; - $author_id = $post->post_author; - - clear_user_posts_count_cache( $author_id, $post_type ); -} - /** * Gets the number of posts a user has written. *