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. if ( ! is_numeric( $post_id ) || $post_id != floor( $post_id ) || ! $post_id ) { 
  162. return false; 
  163.  
  164. $post_id = (int) $post_id; 
  165.  
  166. $_post = wp_cache_get( $post_id, 'posts' ); 
  167.  
  168. if ( ! $_post ) { 
  169. $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) ); 
  170.  
  171. if ( ! $_post ) 
  172. return false; 
  173.  
  174. $_post = sanitize_post( $_post, 'raw' ); 
  175. wp_cache_add( $_post->ID, $_post, 'posts' ); 
  176. } elseif ( empty( $_post->filter ) ) { 
  177. $_post = sanitize_post( $_post, 'raw' ); 
  178.  
  179. return new WP_Post( $_post ); 
  180.  
  181. /** 
  182. * Constructor. 
  183. * @param WP_Post|object $post Post object. 
  184. */ 
  185. public function __construct( $post ) { 
  186. foreach ( get_object_vars( $post ) as $key => $value ) 
  187. $this->$key = $value; 
  188.  
  189. /** 
  190. * Isset-er. 
  191. * @param string $key Property to check if set. 
  192. * @return bool 
  193. */ 
  194. public function __isset( $key ) { 
  195. if ( 'ancestors' == $key ) 
  196. return true; 
  197.  
  198. if ( 'page_template' == $key ) 
  199. return true; 
  200.  
  201. if ( 'post_category' == $key ) 
  202. return true; 
  203.  
  204. if ( 'tags_input' == $key ) 
  205. return true; 
  206.  
  207. return metadata_exists( 'post', $this->ID, $key ); 
  208.  
  209. /** 
  210. * Getter. 
  211. * @param string $key Key to get. 
  212. * @return mixed 
  213. */ 
  214. public function __get( $key ) { 
  215. if ( 'page_template' == $key && $this->__isset( $key ) ) { 
  216. return get_post_meta( $this->ID, '_wp_page_template', true ); 
  217.  
  218. if ( 'post_category' == $key ) { 
  219. if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) 
  220. $terms = get_the_terms( $this, 'category' ); 
  221.  
  222. if ( empty( $terms ) ) 
  223. return array(); 
  224.  
  225. return wp_list_pluck( $terms, 'term_id' ); 
  226.  
  227. if ( 'tags_input' == $key ) { 
  228. if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) 
  229. $terms = get_the_terms( $this, 'post_tag' ); 
  230.  
  231. if ( empty( $terms ) ) 
  232. return array(); 
  233.  
  234. return wp_list_pluck( $terms, 'name' ); 
  235.  
  236. // Rest of the values need filtering. 
  237. if ( 'ancestors' == $key ) 
  238. $value = get_post_ancestors( $this ); 
  239. else 
  240. $value = get_post_meta( $this->ID, $key, true ); 
  241.  
  242. if ( $this->filter ) 
  243. $value = sanitize_post_field( $key, $value, $this->ID, $this->filter ); 
  244.  
  245. return $value; 
  246.  
  247. /** 
  248. * {@Missing Summary} 
  249. * @param string $filter Filter. 
  250. * @return self|array|bool|object|WP_Post 
  251. */ 
  252. public function filter( $filter ) { 
  253. if ( $this->filter == $filter ) 
  254. return $this; 
  255.  
  256. if ( $filter == 'raw' ) 
  257. return self::get_instance( $this->ID ); 
  258.  
  259. return sanitize_post( $this, $filter ); 
  260.  
  261. /** 
  262. * Convert object to array. 
  263. * @return array Object as array. 
  264. */ 
  265. public function to_array() { 
  266. $post = get_object_vars( $this ); 
  267.  
  268. foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) { 
  269. if ( $this->__isset( $key ) ) 
  270. $post[ $key ] = $this->__get( $key ); 
  271.  
  272. return $post;