Using WordPress ‘get_postdata()’ PHP function

The get_postdata() WordPress PHP function retrieves all post data for a given post.

Usage

To use the get_postdata() function, simply provide the post ID as an argument:

$post_data = get_postdata( $post_id );

Parameters

  • $postid (int): The required Post ID for which you want to retrieve the data.

More information

See WordPress Developer Resources: get_postdata()

Examples

Get post data for a specific post

Retrieve post data for a post with the ID 42:

$post_data = get_postdata( 42 );

Display post title

Retrieve post data and display the post title:

$post_data = get_postdata( 42 );
echo 'Post Title: ' . $post_data['Title'];

Display post author

Retrieve post data and display the post author’s display name:

$post_data = get_postdata( 42 );
echo 'Post Author: ' . $post_data['Author_Name'];

Display post content

Retrieve post data and display the post content:

$post_data = get_postdata( 42 );
echo 'Post Content: ' . $post_data['Content'];

Display post excerpt

Retrieve post data and display the post excerpt:

$post_data = get_postdata( 42 );
echo 'Post Excerpt: ' . $post_data['Excerpt'];