get_post()
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.
No Hooks.
Return
WP_Post|Array|null
.
- WP_Post - when the post was successfully retrieved and $output = OBJECT.
array
- when the post was successfully retrieved and $output = ARRAY_A or ARRAY_N.null
- on failure.
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 objectARRAY_A
- associative arrayARRAY_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 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 );
#2 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'];
#3 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 )
Notes
- Global. WP_Post. $post Global post object.
Changelog
Since 1.5.1 | Introduced. |