get_post ( int|WP_Post|null $post = null, string $output = OBJECT, string $filter = 'raw' )

Retrieves post data given a post ID or post object.

See sanitize_post() for optional $filter values. Also, the parameter $post, must be given as a variable, since it is passed by reference.

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.

Source file: wp-includes/post.php

View source


Parameters

$post

(int|WP_Post|null) (Optional) Post ID or post object. Defaults to global $post.

Default value: null

$output

(string) (Optional) default is Object. Accepts OBJECT, ARRAY_A, or ARRAY_N.

Default value: OBJECT

$filter

(string) (Optional) Type of filter to apply. Accepts 'raw', 'edit', 'db', or 'display'.

Default value: 'raw'


Used by


Source

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;
}


Changelog


User Contributed Notes

  1. To get the title for a post with ID 7:

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

    Alternatively, specify the $output parameter:

    $post_7 = get_post( 7, ARRAY_A );
    $title = $post_7['post_title'];
    
  2. Wouldn’t it be better practice to use get_the_title(..) in this case? directly accessing the post object’s data member would bypass applying filters and enforcing protected and private settings, unless that’s explicitly desired.

  3. For reference, WP_Post OBJECT contains following fields:

    WP_Post Object
    (
        [ID] =>
        [post_author] =>
        [post_date] => 
        [post_date_gmt] => 
        [post_content] => 
        [post_title] => 
        [post_excerpt] => 
        [post_status] =>
        [comment_status] =>
        [ping_status] => 
        [post_password] => 
        [post_name] =>
        [to_ping] => 
        [pinged] => 
        [post_modified] => 
        [post_modified_gmt] =>
        [post_content_filtered] => 
        [post_parent] => 
        [guid] => 
        [menu_order] =>
        [post_type] =>
        [post_mime_type] => 
        [comment_count] =>
        [filter] =>
    )
    
  4. For Reference : WP_Post Object has following properties, which are returned by get_post().

    Member Variable Variable Type 	Notes
    ID 	        int 	The ID of the post
    post_author 	string 	The post author's user ID (numeric string)
    post_name 	string 	The post's slug
    post_type 	string 	See Post Types
    post_title 	string 	The title of the post
    post_date 	string 	Format: 0000-00-00 00:00:00
    post_date_gmt 	string 	Format: 0000-00-00 00:00:00
    post_content 	string 	The full content of the post
    post_excerpt 	string 	User-defined post excerpt
    post_status 	string 	See get_post_status for values
    comment_status 	string 	Returns: { open, closed }
    ping_status 	string 	Returns: { open, closed }
    post_password 	string 	Returns empty string if no password
    post_parent 	int 	Parent Post ID (default 0)
    post_modified 	string 	Format: 0000-00-00 00:00:00
    post_modified_gmt 	string 	Format: 0000-00-00 00:00:00
    comment_count 	string 	Number of comments on post (numeric string)
    menu_order 	string 	Order value as set through page-attribute when enabled (numeric string. Defaults to 0) 
    
  5. If you have a shortcode in the content you should use:

    $post = get_post( 4304,ARRAY_A );
    $output =  do_shortcode($post['post_content']);
  6. 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 );
    $output =  apply_filters( 'the_content', $post->post_content );
    

You must log in before being able to contribute a note.