WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get post

Description

Takes a post ID and returns the database record for that post. You can specify, by means of the $output parameter, how you would like the results returned.

Usage

<?php get_post$id$output$filter ); ?> 

Parameters

$id
(integer or object) (optional) The ID of the post you'd like to fetch, or an object that specifies the post. By default the current post is fetched.
Default: null
$output
(string) (optional) How you'd like the result.
  • OBJECT - (default) returns a WP_Post object
  • ARRAY_A - Returns an associative array of field names to values
  • ARRAY_N - returns a numeric array of field values
Default: OBJECT
$filter
(string) (optional) Filter the post. See sanitize_post_field() for a full list of values.
  • raw - (default)
Default: raw

Return Values

Returns a WP_Post object, or null if the post doesnt exist or an error occurred.

Example

To get the title for a post with ID 7:

<?php
$post_7 
get_post(7); 
$title $post_7->post_title;
?> 

Alternatively, specify the $output parameter:

<?php
$post_7 
get_post(7ARRAY_A);
$title $post_7['post_title'];
?> 

Notes

Before version 3.5, the first parameter $post was required to be a variable. For example, get_post(7) would cause a fatal error.

Source File

get_post() is located in wp-includes/post.php and wp-includes/class-wp-atom-server.php.

Change Log

  • Since 1.5.1
  • 3.5.0 - the $post parameter is no longer passed by reference.

Related

See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.