WP_Post::get_instance

Advertisement

Summery Summery

Retrieve WP_Post instance.

Syntax Syntax

WP_Post::get_instance( int $post_id )

Parameters Parameters

$post_id

(Required) Post ID.

Return Return

(WP_Post|false) Post object, false otherwise.

Source Source

File: wp-includes/class-wp-post.php

	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 ) ) {
			$_post = sanitize_post( $_post, 'raw' );
		}

		return new WP_Post( $_post );
	}

Advertisement

Changelog Changelog

Changelog
Version Description
3.5.0 Introduced.

Advertisement

Leave a Reply