_wp_preview_meta_filter()WP 6.4.0

Filters preview post meta retrieval to get values from the autosave.

Filters revisioned meta keys only.

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

No Hooks.

Return

Mixed. Original meta value if the meta key isn't revisioned, the object doesn't exist, the post type is a revision or the post ID doesn't match the object ID. Otherwise, the revisioned meta value is returned for the preview.

Usage

_wp_preview_meta_filter( $value, $object_id, $meta_key, $single );
$value(mixed) (required)
Meta value to filter.
$object_id(int) (required)
Object ID.
$meta_key(string) (required)
Meta key to filter a value for.
$single(true|false) (required)
Whether to return a single value.
Default: false

Changelog

Since 6.4.0 Introduced.

_wp_preview_meta_filter() code WP 6.6.2

function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) {

	$post = get_post();
	if (
		empty( $post ) ||
		$post->ID !== $object_id ||
		! in_array( $meta_key, wp_post_revision_meta_keys( $post->post_type ), true ) ||
		'revision' === $post->post_type
	) {
		return $value;
	}

	$preview = wp_get_post_autosave( $post->ID );
	if ( false === $preview ) {
		return $value;
	}

	return get_post_meta( $preview->ID, $meta_key, $single );
}