3 * Post API: WP_Post class
11 * Core class used to implement the WP_Post object.
15 * @property string $page_template
17 * @property-read array $ancestors
18 * @property-read int $post_category
19 * @property-read string $tag_input
34 * A numeric string, for compatibility reasons.
38 public $post_author = 0;
41 * The post's local publication time.
45 public $post_date = '0000-00-00 00:00:00';
48 * The post's GMT publication time.
52 public $post_date_gmt = '0000-00-00 00:00:00';
59 public $post_content = '';
66 public $post_title = '';
73 public $post_excerpt = '';
80 public $post_status = 'publish';
83 * Whether comments are allowed.
87 public $comment_status = 'open';
90 * Whether pings are allowed.
94 public $ping_status = 'open';
97 * The post's password in plain text.
101 public $post_password = '';
108 public $post_name = '';
111 * URLs queued to be pinged.
115 public $to_ping = '';
118 * URLs that have been pinged.
125 * The post's local modified time.
129 public $post_modified = '0000-00-00 00:00:00';
132 * The post's GMT modified time.
136 public $post_modified_gmt = '0000-00-00 00:00:00';
139 * A utility DB field for post content.
144 public $post_content_filtered = '';
147 * ID of a post's parent post.
151 public $post_parent = 0;
154 * The unique identifier for a post, not necessarily a URL, used as the feed GUID.
161 * A field used for ordering posts.
165 public $menu_order = 0;
168 * The post's type, like post or page.
172 public $post_type = 'post';
175 * An attachment's mime type.
179 public $post_mime_type = '';
182 * Cached comment count.
184 * A numeric string, for compatibility reasons.
188 public $comment_count = 0;
191 * Stores the post object's sanitization level.
193 * Does not correspond to a DB field.
200 * Retrieve WP_Post instance.
205 * @global wpdb $wpdb WordPress database abstraction object.
207 * @param int $post_id Post ID.
208 * @return WP_Post|false Post object, false otherwise.
210 public static function get_instance( $post_id ) {
213 if ( ! is_numeric( $post_id ) || $post_id != floor( $post_id ) || ! $post_id ) {
217 $post_id = (int) $post_id;
219 $_post = wp_cache_get( $post_id, 'posts' );
222 $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
227 $_post = sanitize_post( $_post, 'raw' );
228 wp_cache_add( $_post->ID, $_post, 'posts' );
229 } elseif ( empty( $_post->filter ) ) {
230 $_post = sanitize_post( $_post, 'raw' );
233 return new WP_Post( $_post );
239 * @param WP_Post|object $post Post object.
241 public function __construct( $post ) {
242 foreach ( get_object_vars( $post ) as $key => $value )
243 $this->$key = $value;
249 * @param string $key Property to check if set.
252 public function __isset( $key ) {
253 if ( 'ancestors' == $key )
256 if ( 'page_template' == $key )
259 if ( 'post_category' == $key )
262 if ( 'tags_input' == $key )
265 return metadata_exists( 'post', $this->ID, $key );
271 * @param string $key Key to get.
274 public function __get( $key ) {
275 if ( 'page_template' == $key && $this->__isset( $key ) ) {
276 return get_post_meta( $this->ID, '_wp_page_template', true );
279 if ( 'post_category' == $key ) {
280 if ( is_object_in_taxonomy( $this->post_type, 'category' ) )
281 $terms = get_the_terms( $this, 'category' );
283 if ( empty( $terms ) )
286 return wp_list_pluck( $terms, 'term_id' );
289 if ( 'tags_input' == $key ) {
290 if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) )
291 $terms = get_the_terms( $this, 'post_tag' );
293 if ( empty( $terms ) )
296 return wp_list_pluck( $terms, 'name' );
299 // Rest of the values need filtering.
300 if ( 'ancestors' == $key )
301 $value = get_post_ancestors( $this );
303 $value = get_post_meta( $this->ID, $key, true );
306 $value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
314 * @param string $filter Filter.
315 * @return self|array|bool|object|WP_Post
317 public function filter( $filter ) {
318 if ( $this->filter == $filter )
321 if ( $filter == 'raw' )
322 return self::get_instance( $this->ID );
324 return sanitize_post( $this, $filter );
328 * Convert object to array.
330 * @return array Object as array.
332 public function to_array() {
333 $post = get_object_vars( $this );
335 foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
336 if ( $this->__isset( $key ) )
337 $post[ $key ] = $this->__get( $key );