Make WordPress Core

Opened 7 years ago

Last modified 6 weeks ago

#40711 new enhancement

WP_Post:get_instance: Update cache if $_post->filter is empty

Reported by: greencp's profile greencp Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.7.4
Component: Posts, Post Types Keywords: has-patch
Focuses: performance Cc:


WP_Post::get_instance always returns raw filtered posts. At least in my tests it gets many hits where a post is already cached, but not filtered. For each hit it calls sanitize_post again.

To improve performance, if a cached post isn't filtered, update the cache after calling sanitize_post.

class-wp-post.php, line 228:

Instead of

} elseif ( empty( $_post->filter ) ) {
  $_post = sanitize_post( $_post, 'raw' );

add wp_cache_replace

} elseif ( empty( $_post->filter ) ) {
  $_post = sanitize_post( $_post, 'raw' );
  wp_cache_replace( $_post->ID, $_post, 'posts' );

This significantly reduces unnecessary calls to sanitize_post.

Attachments (1)

40711.diff (466 bytes) - added by Soean 7 years ago.
Implements the code from the description.

Download all attachments as: .zip

Change History (3)

7 years ago

Implements the code from the description.

#1 @Soean
7 years ago

  • Keywords has-patch added

#2 @crstauf
6 weeks ago

Yes please...?

Note: See TracTickets for help on using tickets.