get_post()WP 1.5.1

Gets the data of the current or specified post. You need to specify post ID, or the function will return the current post. Returns a WP_Post object.

It can be any post type: attachment, post, page, any custom type. If no post is specified, the function will get the current post corresponding to the global $post variable.

IMPORTANT! get_post() without parameters returns global $post object! Tested with WP 6.0.

For example:

global $post;
$post = get_post( 1 );

echo $post->post_type; // post

$some_post = get_post();
$some_post->post_type = 'my_post_type';

echo $post->post_type; // my_post_type

Another example. Suppose we already have a defined global $post variable. Now, let's run such code:

$mypost = get_post();
$mypost->post_type = 'my_post_type';

global $post;
echo $post->post_type; // my_post_type

Prior to version 3.5, the first parameter could only receive a variable, a direct transfer of a number caused an error:

//  OK: variable is passed
$post_id = 7;
$the_post = get_post( $post_id );

// ERROR: number is passed
$the_post = get_post( 7 );

From WP 3.5 we can pass a number itself.

Do not confuse with the function get_posts(), which retrive data of many posts.

Use get_page_by_path(), to get post by slug.

1 time — 0.000879 sec (slow) | 50000 times — 0.25 sec (very fast) | PHP 7.2.5, WP 5.0.1

No Hooks.

Return

WP_Post|Array|null. Type corresponding to $output on success or null on failure. When $output is OBJECT, a WP_Post instance is returned.

Usage

$post = get_post( $post, $output, $filter );
$post(int/WP_Post/null)
Post ID or post object, data of which we need to retrive. null — the current post data will be retrived.
Default: global $post
$output(string)

The required return type. Can be:

  • OBJECT - WP_Post object
  • ARRAY_A - associative array
  • ARRAY_N - numeric array.

Default: OBJECT

$filter(string)
How to filter returned post data. See the description of the sanitize_post_field() function for a complete list of values. Accepts 'raw', 'edit', 'db', or 'display'.
Default: 'raw'

Examples

0

#1 Retrive single post data

Get data of the post with ID 7 and get the title of the post:

$post = get_post( 7 );
$title = $post->post_title;

Get data of post 7 in the type of an associative array and "pull out" the title from the array:

$post = get_post( 7, ARRAY_A );
$title = $post['post_title'];
0

#2 WP_Post OBJECT contains following fields:

$post = get_post( 2 );
print_r( $post );
WP_Post Object(
	[ID]                    => 2
	[post_author]           => 1
	[post_date]             => 2018-03-21 17:25:55
	[post_date_gmt]         => 2018-03-21 20:25:55
	[post_content]          => Some text of post content...
	[post_title]            => Contacts
	[post_excerpt]          => 
	[post_status]           => publish
	[comment_status]        => closed
	[ping_status]           => closed
	[post_password]         => 
	[post_name]             => contacts
	[to_ping]               => 
	[pinged]                => 
	[post_modified]         => 2018-04-24 18:52:04
	[post_modified_gmt]     => 2018-04-24 21:52:04
	[post_content_filtered] => 
	[post_parent]           => 0
	[guid]                  => https://site.com/contacts
	[menu_order]            => 0
	[post_type]             => page
	[post_mime_type]        => 
	[comment_count]         => 0
	[filter]                => raw
)
0

#3 Get formatted content of the post

If you need special things — [shortcodes], paragraph tags, anything exciting—in the content, you should apply the filters as opposed to using do_shortcode().

$post = get_post( 42 );
$content = apply_filters( 'the_content', $post->post_content );

Notes

  • Global. WP_Post. $post Global post object.

Changelog

Since 1.5.1 Introduced.

get_post() code WP 6.4.3

function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) {
	if ( empty( $post ) && isset( $GLOBALS['post'] ) ) {
		$post = $GLOBALS['post'];
	}

	if ( $post instanceof WP_Post ) {
		$_post = $post;
	} elseif ( is_object( $post ) ) {
		if ( empty( $post->filter ) ) {
			$_post = sanitize_post( $post, 'raw' );
			$_post = new WP_Post( $_post );
		} elseif ( 'raw' === $post->filter ) {
			$_post = new WP_Post( $post );
		} else {
			$_post = WP_Post::get_instance( $post->ID );
		}
	} else {
		$_post = WP_Post::get_instance( $post );
	}

	if ( ! $_post ) {
		return null;
	}

	$_post = $_post->filter( $filter );

	if ( ARRAY_A === $output ) {
		return $_post->to_array();
	} elseif ( ARRAY_N === $output ) {
		return array_values( $_post->to_array() );
	}

	return $_post;
}