get_post() Function – WordPress

The get_post() function in WordPress is used to retrieve a post object based on the provided post ID. The function returns a WP_Post object that contains all the post data for the specified post, including its ID, title, content, date, author, and more.

Here’s an example of how you could use the get_post() function to retrieve a post and display its title:

$post_id = 123;
$post = get_post( $post_id );

if ( $post ) {
echo '<h2>' . $post->post_title . '</h2>';

In this example, $post_id is set to the ID of the post you want to retrieve, and then get_post() is called with this ID as the argument. The function returns the post object, which is stored in the $post variable. If the post was found, its title is displayed using the post_title property of the $post object.

Note that if the post with the specified ID does not exist, the get_post() function will return NULL, so it’s a good idea to check if the post was found before using its properties.

The get_post() function can be useful in a variety of scenarios, such as when you want to display specific information about a post outside of the loop, or when you want to modify a post’s data before displaying it. You can find more information about the get_post() function and other functions related to retrieving post data in the WordPress Codex: