WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 2 years ago

#40711 new enhancement

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

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

Description

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 2 years ago.
Implements the code from the description.

Download all attachments as: .zip

Change History (2)

@Soean
2 years ago

Implements the code from the description.

#1 @Soean
2 years ago

  • Keywords has-patch added
Note: See TracTickets for help on using tickets.