WP_Metadata_Lazyloader
- Since
- 4.5.0
- Source
wp-includes/class-wp-metadata-lazyloader.php:31
Description
When loading many objects of a given type, such as posts in a WP_Query loop, it often makes sense to prime various metadata caches at the beginning of the loop. This means fetching all relevant metadata with a single database query, a technique that has the potential to improve performance dramatically in some cases.
In cases where the given metadata may not even be used in the loop, we can improve performance even more by only priming the metadata cache for affected items the first time a piece of metadata is requested - ie, by lazy-loading it. So, for example, comment meta may not be loaded into the cache in the comments section of a post until the first time get_comment_meta() is called in the context of the comment loop.
WP uses the WP_MetadataLazyloader class to queue objects for metadata cache priming. The class then detects the relevant get*_meta() function call, and queries the metadata of all queued objects.
Do not access this class directly. Use the wp_metadata_lazyloader() function.
Compatibility
- WordPress
- since 4.5.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Hooks and filters fired · 1
Every hook that fires from inside WP_Metadata_Lazyloader, in the order it appears in the class, grouped by the method that fires it.
Properties · 2
$pending_objectsarrayprotected- Pending objects queue.
$settingsarrayprotected- Settings for supported object types.
Methods · 6
- __construct()Constructor.
- queue_objects()Adds objects to the metadata lazy-load queue.
- reset_queue()Resets lazy-load queue for a given object type.
- lazyload_term_meta()Lazy-loads term meta for queued terms.
- lazyload_comment_meta()Lazy-loads comment meta for queued comments.
- lazyload_meta_callback()Lazy-loads meta for queued objects.
Source code
#[AllowDynamicProperties]class WP_Metadata_Lazyloader { /** * Pending objects queue. * * @since 4.5.0 * @var array */ protected $pending_objects; /** * Settings for supported object types. * * @since 4.5.0 * @var array */ protected $settings = array(); /** * Constructor. * * @since 4.5.0 */ public function __construct() { $this->settings = array( 'term' => array( 'filter' => 'get_term_metadata', 'callback' => array( $this, 'lazyload_meta_callback' ), ), 'comment' => array( 'filter' => 'get_comment_metadata', 'callback' => array( $this, 'lazyload_meta_callback' ), ), 'blog' => array( 'filter' => 'get_blog_metadata', 'callback' => array( $this, 'lazyload_meta_callback' ), ), ); } /** * Adds objects to the metadata lazy-load queue. * * @since 4.5.0 * * @param string $object_type Type of object whose meta is to be lazy-loaded. Accepts 'term' or 'comment'. * @param array $object_ids Array of object IDs. * @return void|WP_Error WP_Error on failure. */ public function queue_objects( $object_type, $object_ids ) { if ( ! isset( $this->settings[ $object_type ] ) ) { return new WP_Error( 'invalid_object_type', __( 'Invalid object type.' ) ); } $type_settings = $this->settings[ $object_type ]; if ( ! isset( $this->pending_objects[ $object_type ] ) ) { $this->pending_objects[ $object_type ] = array(); } foreach ( $object_ids as $object_id ) { // Keyed by ID for faster lookup. if ( ! isset( $this->pending_objects[ $object_type ][ $object_id ] ) ) { $this->pending_objects[ $object_type ][ $object_id ] = 1; } } add_filter( $type_settings['filter'], $type_settings['callback'], 10, 5 ); /** * Fires after objects are added to the metadata lazy-load queue. * * @since 4.5.0 * * @param array $object_ids Array of object IDs. * @param string $object_type Type of object being queued. * @param WP_Metadata_Lazyloader $lazyloader The lazy-loader object. */ do_action( 'metadata_lazyloader_queued_objects', $object_ids, $object_type, $this ); }Changelog
Introduced in 4.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-metadata-lazyloader.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.