Skip to content

Commit b394c9c

Browse files
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
1 parent cc8dd3e commit b394c9c

3 files changed

Lines changed: 7 additions & 7 deletions

File tree

src/wp-includes/ms-blogs.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -877,12 +877,12 @@ function _update_blog_date_on_post_delete( $post_id ) {
877877
* Handler for updating the current site's posts count when a post is deleted.
878878
*
879879
* @since 4.0.0
880+
* @since 6.2.0 Added the `$post` parameter.
880881
*
881-
* @param int $post_id Post ID.
882+
* @param int $post_id Post ID.
883+
* @param WP_Post $post Post object.
882884
*/
883-
function _update_posts_count_on_delete( $post_id ) {
884-
$post = get_post( $post_id );
885-
885+
function _update_posts_count_on_delete( $post_id, $post ) {
886886
if ( ! $post || 'publish' !== $post->post_status || 'post' !== $post->post_type ) {
887887
return;
888888
}

src/wp-includes/ms-default-filters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
add_filter( 'allowed_redirect_hosts', 'redirect_this_site' );
7676

7777
// Administration.
78-
add_action( 'after_delete_post', '_update_posts_count_on_delete' );
78+
add_action( 'after_delete_post', '_update_posts_count_on_delete', 10, 2 );
7979
add_action( 'delete_post', '_update_blog_date_on_post_delete' );
8080
add_action( 'transition_post_status', '_update_blog_date_on_post_publish', 10, 3 );
8181
add_action( 'transition_post_status', '_update_posts_count_on_transition_post_status', 10, 3 );

tests/phpunit/tests/multisite/updatePostsCount.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function test_update_posts_count() {
3030

3131
$post_count_after_creating = get_site()->post_count;
3232

33-
wp_delete_post( $post_id );
33+
wp_delete_post( $post_id, true );
3434

3535
$post_count_after_deleting = get_site()->post_count;
3636

@@ -47,7 +47,7 @@ public function test_update_posts_count() {
4747

4848
/*
4949
* Check that posts count is updated when a post is deleted:
50-
* add_action( 'deleted_post', '_update_posts_count_on_delete' );
50+
* add_action( 'after_delete_post', '_update_posts_count_on_delete', 10, 2 );
5151
*
5252
* Check that _update_posts_count_on_delete() is called on that filter,
5353
* which then calls update_posts_count() to update the count.

0 commit comments

Comments
 (0)