On this page
WP_Post::get_instance( int $post_id ): WP_Post|false
Retrieve WP_Post instance.
Parameters
$post_idint Required-
Post ID.
Return
WP_Post|false Post object, false otherwise.
Source
File: wp-includes/class-wp-post.php. View all references
public static function get_instance( $post_id ) {
global $wpdb;
$post_id = (int) $post_id;
if ( ! $post_id ) {
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 ) );
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 );
}
Related
Uses
| Uses | Description |
|---|---|
| wp_cache_add() wp-includes/cache.php | Adds data to the cache, if the cache key doesn’t already exist. |
| WP_Post::__construct() wp-includes/class-wp-post.php | Constructor. |
| sanitize_post() wp-includes/post.php | Sanitizes every post field. |
| wpdb::get_row() wp-includes/class-wpdb.php | Retrieves one row from the database. |
| wp_cache_get() wp-includes/cache.php | Retrieves the cache contents from the cache by key and group. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
Used By
| Used By | Description |
|---|---|
| WP_Post::filter() wp-includes/class-wp-post.php | {@Missing Summary} |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Changelog
| Version | Description |
|---|---|
| 3.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_post/get_instance