@@ -231,27 +231,51 @@ final class WP_Post {
231231 */
232232 public static function get_instance ( $ post_id ) {
233233 global $ wpdb ;
234-
234+
235235 $ post_id = (int ) $ post_id ;
236+
237+ /**
238+ * Filters the returned value for WP_Post::get_instance().
239+ *
240+ * Allows plugins/themes to return a faux or virtual WP_Post object
241+ * before WordPress queries the database.
242+ *
243+ * Returning a non-null value short-circuits the normal execution.
244+ *
245+ * @since 6.x.x
246+ *
247+ * @param WP_Post|object|null $post A custom/faux post object or null.
248+ * @param int $post_id Requested post ID.
249+ */
250+ $ pre = apply_filters ( 'pre_wp_post_get_instance ' , null , $ post_id );
251+
252+ if ( null !== $ pre ) {
253+ // Ensure the result is a proper WP_Post object.
254+ return ( $ pre instanceof WP_Post ) ? $ pre : new WP_Post ( $ pre );
255+ }
256+
257+ // Existing logic (unchanged)
236258 if ( $ post_id <= 0 ) {
237259 return false ;
238260 }
239-
261+
240262 $ _post = wp_cache_get ( $ post_id , 'posts ' );
241-
263+
242264 if ( ! $ _post ) {
243- $ _post = $ wpdb ->get_row ( $ wpdb ->prepare ( "SELECT * FROM $ wpdb ->posts WHERE ID = %d LIMIT 1 " , $ post_id ) );
244-
265+ $ _post = $ wpdb ->get_row (
266+ $ wpdb ->prepare ( "SELECT * FROM $ wpdb ->posts WHERE ID = %d LIMIT 1 " , $ post_id )
267+ );
268+
245269 if ( ! $ _post ) {
246270 return false ;
247271 }
248-
272+
249273 $ _post = sanitize_post ( $ _post , 'raw ' );
250274 wp_cache_add ( $ _post ->ID , $ _post , 'posts ' );
251275 } elseif ( empty ( $ _post ->filter ) || 'raw ' !== $ _post ->filter ) {
252276 $ _post = sanitize_post ( $ _post , 'raw ' );
253277 }
254-
278+
255279 return new WP_Post ( $ _post );
256280 }
257281
0 commit comments