[wp-trac] [WordPress Trac] #27952: get_blog_details()->post_count Does not subtract deleted posts

WordPress Trac noreply at wordpress.org
Sun Jun 8 20:35:54 UTC 2014


#27952: get_blog_details()->post_count Does not subtract deleted posts
--------------------------------------+------------------------
 Reporter:  GSMcNamara                |       Owner:
     Type:  defect (bug)              |      Status:  new
 Priority:  normal                    |   Milestone:  4.0
Component:  Networks and Sites        |     Version:  3.8.1
 Severity:  minor                     |  Resolution:
 Keywords:  good-first-bug has-patch  |     Focuses:  multisite
--------------------------------------+------------------------

Comment (by strangerstudios):

 Okay. Attached a new diff. Changes:

 I left the `update_posts_count()` function in `ms-functions.php` alone,
 except to change the wording of the comment. It is slightly different from
 the first patch but I think reflects what this latest patch does.

 I added two new functions to `ms-blogs.php` mimicking the
 `_update_blog_date_on_post_publish()` and
 `_update_blog_date_on_post_delete()` functions. I wouldn't want to add new
 functions here if we didn't have to, but we need to do something like this
 so we can check if the status is really changing. Added these two
 functions also has the advantages of having clearer code and preserving
 the update_posts_count() function to simply update the count with no
 checks if people are using the function that way.

 1. `_update_posts_count_on_delete($post_id)` now fires on `post_delete`.
 If the post being deleted is not in "publish" status, we don't update the
 count since deleting a draft/etc wouldn't affect the count.

 2. `_update_posts_count_on_transition_post_status($new_status,
 $old_status)` now fires on `transition_post_status`. If the post statuses
 are the same or neither of them is "publish", we don't update the count
 since this change shouldn't affect the count.

 I tested on my multisite dev and it successfully updated the count when
 posts were created, deleted, updated, etc, and didn't update the count in
 the new cases where we don't. This will avoid potentially slow queries on
 sites with lots of published posts.

 If adding so much code for this feels icky, I'd suggest another patch that
 calls `update_posts_count()` on `post_delete` and `save_post` (might as
 well use this since we wouldn't be checking the statuses anyway) with an
 update to the comment RE when `update_posts_count()` is called.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/27952#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list