Posts, Post Types: Pass the post object to _update_posts_count_on_delete().

The function checks the status of the post being deleted, and then only calls `update_posts_count()` if the deleted post was previously published, as the update query would be unnecessary otherwise.

However, by the time the function runs, the post is already deleted from the database, and the post status check fails.

This commit uses the previously retrieved post object for the status check, so that the function proceeds as expected.

Includes updating the unit test to call `wp_delete_post()` with the `$force_delete` argument, so that the post is actually deleted, not trashed, and the `after_delete_post` action is run.

Follow-up to [28835], [52207], [54760], [54762].

Fixes #57023.

git-svn-id: https://develop.svn.wordpress.org/trunk@55419 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2023-02-24 01:21:54 +00:00
parent cc8dd3e1f6
commit b394c9c144
3 changed files with 7 additions and 7 deletions

View File

@@ -877,12 +877,12 @@ function _update_blog_date_on_post_delete( $post_id ) {
* Handler for updating the current site's posts count when a post is deleted.
*
* @since 4.0.0
* @since 6.2.0 Added the `$post` parameter.
*
* @param int $post_id Post ID.
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
*/
function _update_posts_count_on_delete( $post_id ) {
$post = get_post( $post_id );
function _update_posts_count_on_delete( $post_id, $post ) {
if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
return;
}

View File

@@ -75,7 +75,7 @@ add_action( 'template_redirect', 'maybe_redirect_404' );
add_filter( 'allowed_redirect_hosts', 'redirect_this_site' );
// Administration.
add_action( 'after_delete_post', '_update_posts_count_on_delete' );
add_action( 'after_delete_post', '_update_posts_count_on_delete', 10, 2 );
add_action( 'delete_post', '_update_blog_date_on_post_delete' );
add_action( 'transition_post_status', '_update_blog_date_on_post_publish', 10, 3 );
add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 3 );

View File

@@ -30,7 +30,7 @@ if ( is_multisite() ) :
$post_count_after_creating = get_site()->post_count;
wp_delete_post( $post_id );
wp_delete_post( $post_id, true );
$post_count_after_deleting = get_site()->post_count;
@@ -47,7 +47,7 @@ if ( is_multisite() ) :
/*
* Check that posts count is updated when a post is deleted:
* add_action( 'deleted_post', '_update_posts_count_on_delete' );
* add_action( 'after_delete_post', '_update_posts_count_on_delete', 10, 2 );
*
* Check that _update_posts_count_on_delete() is called on that filter,
* which then calls update_posts_count() to update the count.