From 7537a245a99fb9c1d06cab27c515e82b5741de24 Mon Sep 17 00:00:00 2001 From: iFlair Date: Fri, 5 Dec 2025 13:52:51 +0530 Subject: [PATCH] Fixed : Ticket 64362 Support for Faux WP_Post Objects --- src/wp-includes/class-wp-post.php | 38 +++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-post.php b/src/wp-includes/class-wp-post.php index 7c8a71eee2fc4..b2434397f27e1 100644 --- a/src/wp-includes/class-wp-post.php +++ b/src/wp-includes/class-wp-post.php @@ -231,27 +231,51 @@ final class WP_Post { */ public static function get_instance( $post_id ) { global $wpdb; - + $post_id = (int) $post_id; + + /** + * Filters the returned value for WP_Post::get_instance(). + * + * Allows plugins/themes to return a faux or virtual WP_Post object + * before WordPress queries the database. + * + * Returning a non-null value short-circuits the normal execution. + * + * @since 6.x.x + * + * @param WP_Post|object|null $post A custom/faux post object or null. + * @param int $post_id Requested post ID. + */ + $pre = apply_filters( 'pre_wp_post_get_instance', null, $post_id ); + + if ( null !== $pre ) { + // Ensure the result is a proper WP_Post object. + return ( $pre instanceof WP_Post ) ? $pre : new WP_Post( $pre ); + } + + // Existing logic (unchanged) if ( $post_id <= 0 ) { return false; } - + $_post = wp_cache_get( $post_id, 'posts' ); - + if ( ! $_post ) { - $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) ); - + $_post = $wpdb->get_row( + $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) + ); + if ( ! $_post ) { return false; } - + $_post = sanitize_post( $_post, 'raw' ); wp_cache_add( $_post->ID, $_post, 'posts' ); } elseif ( empty( $_post->filter ) || 'raw' !== $_post->filter ) { $_post = sanitize_post( $_post, 'raw' ); } - + return new WP_Post( $_post ); }