* Post API: WP_Post class
* Core class used to implement the WP_Post object.
* @property string $page_template
* @property-read int[] $ancestors
* @property-read int $post_category
* @property-read string $tag_input
* A numeric string, for compatibility reasons.
* The post's local publication time.
public $post_date = '0000-00-00 00:00:00';
* The post's GMT publication time.
public $post_date_gmt = '0000-00-00 00:00:00';
public $post_content = '';
public $post_excerpt = '';
public $post_status = 'publish';
* Whether comments are allowed.
public $comment_status = 'open';
* Whether pings are allowed.
public $ping_status = 'open';
* The post's password in plain text.
public $post_password = '';
* URLs queued to be pinged.
* URLs that have been pinged.
* The post's local modified time.
public $post_modified = '0000-00-00 00:00:00';
* The post's GMT modified time.
public $post_modified_gmt = '0000-00-00 00:00:00';
* A utility DB field for post content.
public $post_content_filtered = '';
* ID of a post's parent post.
* The unique identifier for a post, not necessarily a URL, used as the feed GUID.
* A field used for ordering posts.
* The post's type, like post or page.
public $post_type = 'post';
* An attachment's mime type.
public $post_mime_type = '';
* A numeric string, for compatibility reasons.
public $comment_count = 0;
* Stores the post object's sanitization level.
* Does not correspond to a DB field.
* Retrieve WP_Post instance.
* @global wpdb $wpdb WordPress database abstraction object.
* @param int $post_id Post ID.
* @return WP_Post|false Post object, false otherwise.
public static function get_instance( $post_id ) {
$post_id = (int) $post_id;
$_post = wp_cache_get( $post_id, 'posts' );
$_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
$_post = sanitize_post( $_post, 'raw' );
wp_cache_add( $_post->ID, $_post, 'posts' );
} elseif ( empty( $_post->filter ) ) {
$_post = sanitize_post( $_post, 'raw' );
return new WP_Post( $_post );
* @param WP_Post|object $post Post object.
public function __construct( $post ) {
foreach ( get_object_vars( $post ) as $key => $value ) {
* @param string $key Property to check if set.
public function __isset( $key ) {
if ( 'ancestors' === $key ) {
if ( 'page_template' === $key ) {
if ( 'post_category' === $key ) {
if ( 'tags_input' === $key ) {
return metadata_exists( 'post', $this->ID, $key );
* @param string $key Key to get.
public function __get( $key ) {
if ( 'page_template' === $key && $this->__isset( $key ) ) {
return get_post_meta( $this->ID, '_wp_page_template', true );
if ( 'post_category' === $key ) {
if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) {
$terms = get_the_terms( $this, 'category' );
return wp_list_pluck( $terms, 'term_id' );
if ( 'tags_input' === $key ) {
if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) {
$terms = get_the_terms( $this, 'post_tag' );
return wp_list_pluck( $terms, 'name' );
// Rest of the values need filtering.
if ( 'ancestors' === $key ) {
$value = get_post_ancestors( $this );
$value = get_post_meta( $this->ID, $key, true );
$value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
* @param string $filter Filter.
public function filter( $filter ) {
if ( $this->filter === $filter ) {
if ( 'raw' === $filter ) {
return self::get_instance( $this->ID );
return sanitize_post( $this, $filter );
* Convert object to array.
* @return array Object as array.
public function to_array() {
$post = get_object_vars( $this );
foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
if ( $this->__isset( $key ) ) {
$post[ $key ] = $this->__get( $key );