#53443 closed defect (bug) (fixed)
The value of the `post_count` meta field is wrong because it doesn't get decremented when a post is deleted
Reported by: | henry.wright | Owned by: | audrasjb |
---|---|---|---|
Milestone: | 5.9 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Posts, Post Types | Keywords: | has-patch has-unit-tests |
Focuses: | multisite | Cc: |
Description
The update_posts_count()
function is used to update a blog's post count. When a post is added to the database the post_count
value is incremented as expected. However, when a post is deleted, the post_count
value isn't decremented.
This is because the _update_posts_count_on_delete()
function, which calls update_posts_count()
is hooked to the delete_post
action.
delete_post
fires immediately before a post is deleted from the database.
Therefore, the query performed in update_posts_count()
to get the post count is run at the wrong time.
The solution is to use the deleted_post
hook instead of the delete_post
hook.
Attachments (1)
Change History (35)
#2
@
3 years ago
- Component changed from General to Posts, Post Types
- Focuses multisite added
- Milestone changed from Awaiting Review to 5.9
#3
@
3 years ago
- Keywords needs-testing added
Hmm the change makes sense to me. Marking as needs-testing
to ensure reproducible and then fixed after the patch. A test report will be helpful.
This ticket was mentioned in Slack in #core-test by hellofromtonya. View the logs.
3 years ago
This ticket was mentioned in PR #1890 on WordPress/wordpress-develop by pbearne.
3 years ago
#6
- Keywords has-unit-tests added
Trac ticket: [](https://core.trac.wordpress.org/ticket/53443)
#9
@
3 years ago
I am confused why the tests are failing on github as the test is passing in my local
This ticket was mentioned in Slack in #core by audrasjb. View the logs.
3 years ago
This ticket was mentioned in Slack in #core-test by pbearne. View the logs.
3 years ago
3 years ago
#12
It looks like the last commit (moving the action from deleted_post
to after_delete_post
) fixes the issue. It also makes sense to me since now caches are refreshed before assuming the delete action is done.
(the multisite testing report look unrelated)
#14
@
3 years ago
For the failing tests, do we need to create a blog and switch to it before the assertions are run?
$blog_id = self::factory()->blog->create(); switch_to_blog( $blog_id );
#16
@
3 years ago
In that case the problem could be the array argument passed to the create()
method.
array( 'post_type' => 'post', 'post_author' => '1', 'post_date' => '2012-10-23 19:34:42', )
This will likely be creating a post in draft status because the default value for post_status
is draft.
We could try explicitly setting the post_status
value to publish. update_posts_count()
gets a count of published posts only to set the post count.
array( 'post_type' => 'post', 'post_author' => '1', 'post_date' => '2012-10-23 19:34:42', 'post_status' => 'publish', )
#17
@
3 years ago
Also when a new WordPress blog is created, it is automatically populated with Hello world! post. Do we need to account for that post in the assertion?
#18
@
3 years ago
Actually, I can see the automatically populated Hello world! post is already accounted for
henrywright commented on PR #1890:
3 years ago
#19
@audrasjb tests still failing now b17e1e7 is applied means the value of post_count
isn't incremented when the Hello world! post is created during blog creation
The Hello world! post is inserted using $wpdb
methods instead of going through the wp_insert_post()
API. Because the API isn't used, update_posts_count()
isn't called. This results in the assertion failing because the expected value is erroneously 1. The expected value should be, and would be, 2 if the Hello world! post incremented post_count
.
I'll post the specific test failure below for easy reference:
There was 1 failure: 1) Tests_update_posts_count_on_delete::test_update_posts_count_on_delete post added Failed asserting that 2 matches expected 1. /var/www/tests/phpunit/tests/multisite/ms-functions/update_posts_count.php:38
3 years ago
#20
Alright, let's try to take into account the "Hello World" blog post automatically created with the blog then.
henrywright commented on PR #1890:
3 years ago
#21
Yay tests are passing
1 final amendment I'd suggest is add a call to restore_current_blog()
at the end of the routine because we added a switch_to_blog()
call at the start
#22
@
3 years ago
- Keywords commit added
Tests are passing now. Self-assigning for commit
consideration.
#26
@
3 years ago
- Keywords needs-refresh added; commit removed
Now that #54462 is closed, let's refresh the unit tests.
3 years ago
#28
@henrywright the code is updated. Tests are passing.
I think we're good to go now, isn't it?
This ticket was mentioned in PR #1912 on WordPress/wordpress-develop by audrasjb.
3 years ago
#29
- Keywords needs-refresh removed
Trac ticket: https://core.trac.wordpress.org/ticket/53443
#30
@
3 years ago
@henrywright @pbearne I added a new PR to rename the new unit test file from update_posts_count.php
to updatePostsCount.php
for better consistency with the other test files in this folder.
3 years ago
#33
Committed in https://core.trac.wordpress.org/changeset/52207
hellofromtonya commented on PR #1912:
3 years ago
#34
Committed via https://core.trac.wordpress.org/changeset/52207.
53443.diff uses the deleted_post hook instead of the delete_post hook