WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

#42021 closed defect (bug) (fixed)

_update_posts_count_on_transition_post_status firing for incorrect post types

Reported by: sboisvert Owned by: jeremyfelt
Milestone: 4.9 Priority: normal
Severity: normal Version: 4.0
Component: Networks and Sites Keywords: has-patch needs-unit-tests
Focuses: multisite Cc:

Description

In a Multisite environment the number of posts (of the posts post_type) is tracked by update_posts_count(). This function will do a large select query which can be pretty slow. The problem is that currently _update_posts_count_on_transition_post_status() will trigger no matter what post type is being transitioned, since update_posts_count() only counts the posts post_type we don't need to trigger a count when a post of another post_type is transitioning.

This patch adds the 3rd parameter to the _update_posts_count_on_transition_post_status() and then does a check and returns if the post is not the post post_type

Attachments (4)

post_counts.diff (1.6 KB) - added by sboisvert 2 years ago.
42021.patch (1.8 KB) - added by JPry 2 years ago.
Tweaks to @sboisvert 's patch
42021.diff (1.7 KB) - added by spacedmonkey 2 years ago.
42021.2.diff (2.0 KB) - added by jeremyfelt 2 years ago.

Download all attachments as: .zip

Change History (9)

@JPry
2 years ago

Tweaks to @sboisvert 's patch

#1 @JPry
2 years ago

  • Keywords has-patch added

@spacedmonkey
2 years ago

#2 @spacedmonkey
2 years ago

  • Keywords needs-unit-tests added

Added some improvements. For backward compatiblity, I added null to the $posts param to make sure if anyone is calling the function in there own code there is no breakage.

Also used get_post_type() as it always returns something.

#3 @jeremyfelt
2 years ago

  • Milestone changed from Awaiting Review to 4.9
  • Owner set to jeremyfelt
  • Status changed from new to reviewing

Good catch, thanks @sboisvert - this seems like a straight forward fix.

@jeremyfelt
2 years ago

#4 @jeremyfelt
2 years ago

42021.2.diff applies the same restriction to _update_posts_count_on_delete(), which already has access to the post object.

#5 @jeremyfelt
2 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 41665:

Multisite: Only update a site's post count when post types of post are updated.

Previously, the query to update the count of published posts would run every time any post type transitioned between a publish/non-published status or was deleted.

Props sboisvert, JPry, spacedmonkey.
Fixes #42021.

Note: See TracTickets for help on using tickets.