WP_Post

Core class used to implement the WP_Post object.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-post.php  
  1. final class WP_Post { 
  2.  
  3. /** 
  4. * Post ID. 
  5. * @var int 
  6. */ 
  7. public $ID; 
  8.  
  9. /** 
  10. * ID of post author. 
  11. * A numeric string, for compatibility reasons. 
  12. * @var string 
  13. */ 
  14. public $post_author = 0; 
  15.  
  16. /** 
  17. * The post's local publication time. 
  18. * @var string 
  19. */ 
  20. public $post_date = '0000-00-00 00:00:00'; 
  21.  
  22. /** 
  23. * The post's GMT publication time. 
  24. * @var string 
  25. */ 
  26. public $post_date_gmt = '0000-00-00 00:00:00'; 
  27.  
  28. /** 
  29. * The post's content. 
  30. * @var string 
  31. */ 
  32. public $post_content = ''; 
  33.  
  34. /** 
  35. * The post's title. 
  36. * @var string 
  37. */ 
  38. public $post_title = ''; 
  39.  
  40. /** 
  41. * The post's excerpt. 
  42. * @var string 
  43. */ 
  44. public $post_excerpt = ''; 
  45.  
  46. /** 
  47. * The post's status. 
  48. * @var string 
  49. */ 
  50. public $post_status = 'publish'; 
  51.  
  52. /** 
  53. * Whether comments are allowed. 
  54. * @var string 
  55. */ 
  56. public $comment_status = 'open'; 
  57.  
  58. /** 
  59. * Whether pings are allowed. 
  60. * @var string 
  61. */ 
  62. public $ping_status = 'open'; 
  63.  
  64. /** 
  65. * The post's password in plain text. 
  66. * @var string 
  67. */ 
  68. public $post_password = ''; 
  69.  
  70. /** 
  71. * The post's slug. 
  72. * @var string 
  73. */ 
  74. public $post_name = ''; 
  75.  
  76. /** 
  77. * URLs queued to be pinged. 
  78. * @var string 
  79. */ 
  80. public $to_ping = ''; 
  81.  
  82. /** 
  83. * URLs that have been pinged. 
  84. * @var string 
  85. */ 
  86. public $pinged = ''; 
  87.  
  88. /** 
  89. * The post's local modified time. 
  90. * @var string 
  91. */ 
  92. public $post_modified = '0000-00-00 00:00:00'; 
  93.  
  94. /** 
  95. * The post's GMT modified time. 
  96. * @var string 
  97. */ 
  98. public $post_modified_gmt = '0000-00-00 00:00:00'; 
  99.  
  100. /** 
  101. * A utility DB field for post content. 
  102. * @var string 
  103. */ 
  104. public $post_content_filtered = ''; 
  105.  
  106. /** 
  107. * ID of a post's parent post. 
  108. * @var int 
  109. */ 
  110. public $post_parent = 0; 
  111.  
  112. /** 
  113. * The unique identifier for a post, not necessarily a URL, used as the feed GUID. 
  114. * @var string 
  115. */ 
  116. public $guid = ''; 
  117.  
  118. /** 
  119. * A field used for ordering posts. 
  120. * @var int 
  121. */ 
  122. public $menu_order = 0; 
  123.  
  124. /** 
  125. * The post's type, like post or page. 
  126. * @var string 
  127. */ 
  128. public $post_type = 'post'; 
  129.  
  130. /** 
  131. * An attachment's mime type. 
  132. * @var string 
  133. */ 
  134. public $post_mime_type = ''; 
  135.  
  136. /** 
  137. * Cached comment count. 
  138. * A numeric string, for compatibility reasons. 
  139. * @var string 
  140. */ 
  141. public $comment_count = 0; 
  142.  
  143. /** 
  144. * Stores the post object's sanitization level. 
  145. * Does not correspond to a DB field. 
  146. * @var string 
  147. */ 
  148. public $filter; 
  149.  
  150. /** 
  151. * Retrieve WP_Post instance. 
  152. * @static 
  153. * @access public 
  154. * @global wpdb $wpdb WordPress database abstraction object. 
  155. * @param int $post_id Post ID. 
  156. * @return WP_Post|false Post object, false otherwise. 
  157. */ 
  158. public static function get_instance( $post_id ) { 
  159. global $wpdb; 
  160.  
  161. $post_id = (int) $post_id; 
  162. if ( ! $post_id ) 
  163. return false; 
  164.  
  165. $_post = wp_cache_get( $post_id, 'posts' ); 
  166.  
  167. if ( ! $_post ) { 
  168. $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) ); 
  169.  
  170. if ( ! $_post ) 
  171. return false; 
  172.  
  173. $_post = sanitize_post( $_post, 'raw' ); 
  174. wp_cache_add( $_post->ID, $_post, 'posts' ); 
  175. } elseif ( empty( $_post->filter ) ) { 
  176. $_post = sanitize_post( $_post, 'raw' ); 
  177.  
  178. return new WP_Post( $_post ); 
  179.  
  180. /** 
  181. * Constructor. 
  182. * @param WP_Post|object $post Post object. 
  183. */ 
  184. public function __construct( $post ) { 
  185. foreach ( get_object_vars( $post ) as $key => $value ) 
  186. $this->$key = $value; 
  187.  
  188. /** 
  189. * Isset-er. 
  190. * @param string $key Property to check if set. 
  191. * @return bool 
  192. */ 
  193. public function __isset( $key ) { 
  194. if ( 'ancestors' == $key ) 
  195. return true; 
  196.  
  197. if ( 'page_template' == $key ) 
  198. return ( 'page' == $this->post_type ); 
  199.  
  200. if ( 'post_category' == $key ) 
  201. return true; 
  202.  
  203. if ( 'tags_input' == $key ) 
  204. return true; 
  205.  
  206. return metadata_exists( 'post', $this->ID, $key ); 
  207.  
  208. /** 
  209. * Getter. 
  210. * @param string $key Key to get. 
  211. * @return mixed 
  212. */ 
  213. public function __get( $key ) { 
  214. if ( 'page_template' == $key && $this->__isset( $key ) ) { 
  215. return get_post_meta( $this->ID, '_wp_page_template', true ); 
  216.  
  217. if ( 'post_category' == $key ) { 
  218. if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) 
  219. $terms = get_the_terms( $this, 'category' ); 
  220.  
  221. if ( empty( $terms ) ) 
  222. return array(); 
  223.  
  224. return wp_list_pluck( $terms, 'term_id' ); 
  225.  
  226. if ( 'tags_input' == $key ) { 
  227. if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) 
  228. $terms = get_the_terms( $this, 'post_tag' ); 
  229.  
  230. if ( empty( $terms ) ) 
  231. return array(); 
  232.  
  233. return wp_list_pluck( $terms, 'name' ); 
  234.  
  235. // Rest of the values need filtering. 
  236. if ( 'ancestors' == $key ) 
  237. $value = get_post_ancestors( $this ); 
  238. else 
  239. $value = get_post_meta( $this->ID, $key, true ); 
  240.  
  241. if ( $this->filter ) 
  242. $value = sanitize_post_field( $key, $value, $this->ID, $this->filter ); 
  243.  
  244. return $value; 
  245.  
  246. /** 
  247. * {@Missing Summary} 
  248. * @param string $filter Filter. 
  249. * @return self|array|bool|object|WP_Post 
  250. */ 
  251. public function filter( $filter ) { 
  252. if ( $this->filter == $filter ) 
  253. return $this; 
  254.  
  255. if ( $filter == 'raw' ) 
  256. return self::get_instance( $this->ID ); 
  257.  
  258. return sanitize_post( $this, $filter ); 
  259.  
  260. /** 
  261. * Convert object to array. 
  262. * @return array Object as array. 
  263. */ 
  264. public function to_array() { 
  265. $post = get_object_vars( $this ); 
  266.  
  267. foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) { 
  268. if ( $this->__isset( $key ) ) 
  269. $post[ $key ] = $this->__get( $key ); 
  270.  
  271. return $post;