WordPress at a glance

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.

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

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.

Works based on: WP_Post::get_instance()
✈ 1 time = 0.000185s = fast | 50000 times = 0.19s = very fast PHP 7.1.0, WP 4.7

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

#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'];

#2 Look at the returned data

$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
)

Code of get_post: wp-includes/post.php VER 4.9.8

<?php
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 ( $output == ARRAY_A )
		return $_post->to_array();
	elseif ( $output == ARRAY_N )
		return array_values( $_post->to_array() );

	return $_post;
}

Related Functions

From category: Queries

No comments
    Hello, !     Log In . Register