[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-post.php (source)

   1  <?php
   2  /**
   3   * Post API: WP_Post class
   4   *
   5   * @package WordPress
   6   * @subpackage Post
   7   * @since 4.4.0
   8   */
   9  
  10  /**
  11   * Core class used to implement the WP_Post object.
  12   *
  13   * @since 3.5.0
  14   *
  15   * @property string $page_template
  16   *
  17   * @property-read int[]    $ancestors
  18   * @property-read int[]    $post_category
  19   * @property-read string[] $tags_input
  20   */
  21  #[AllowDynamicProperties]
  22  final class WP_Post {
  23  
  24      /**
  25       * Post ID.
  26       *
  27       * @since 3.5.0
  28       * @var int
  29       */
  30      public $ID;
  31  
  32      /**
  33       * ID of post author.
  34       *
  35       * A numeric string, for compatibility reasons.
  36       *
  37       * @since 3.5.0
  38       * @var string
  39       * @phpstan-var numeric-string
  40       */
  41      public $post_author = '0';
  42  
  43      /**
  44       * The post's local publication time.
  45       *
  46       * @since 3.5.0
  47       * @var string
  48       */
  49      public $post_date = '0000-00-00 00:00:00';
  50  
  51      /**
  52       * The post's GMT publication time.
  53       *
  54       * @since 3.5.0
  55       * @var string
  56       */
  57      public $post_date_gmt = '0000-00-00 00:00:00';
  58  
  59      /**
  60       * The post's content.
  61       *
  62       * @since 3.5.0
  63       * @var string
  64       */
  65      public $post_content = '';
  66  
  67      /**
  68       * The post's title.
  69       *
  70       * @since 3.5.0
  71       * @var string
  72       */
  73      public $post_title = '';
  74  
  75      /**
  76       * The post's excerpt.
  77       *
  78       * @since 3.5.0
  79       * @var string
  80       */
  81      public $post_excerpt = '';
  82  
  83      /**
  84       * The post's status.
  85       *
  86       * @since 3.5.0
  87       * @var string
  88       */
  89      public $post_status = 'publish';
  90  
  91      /**
  92       * Whether comments are allowed.
  93       *
  94       * @since 3.5.0
  95       * @var string
  96       */
  97      public $comment_status = 'open';
  98  
  99      /**
 100       * Whether pings are allowed.
 101       *
 102       * @since 3.5.0
 103       * @var string
 104       */
 105      public $ping_status = 'open';
 106  
 107      /**
 108       * The post's password in plain text.
 109       *
 110       * @since 3.5.0
 111       * @var string
 112       */
 113      public $post_password = '';
 114  
 115      /**
 116       * The post's slug.
 117       *
 118       * @since 3.5.0
 119       * @var string
 120       */
 121      public $post_name = '';
 122  
 123      /**
 124       * URLs queued to be pinged.
 125       *
 126       * @since 3.5.0
 127       * @var string
 128       */
 129      public $to_ping = '';
 130  
 131      /**
 132       * URLs that have been pinged.
 133       *
 134       * @since 3.5.0
 135       * @var string
 136       */
 137      public $pinged = '';
 138  
 139      /**
 140       * The post's local modified time.
 141       *
 142       * @since 3.5.0
 143       * @var string
 144       */
 145      public $post_modified = '0000-00-00 00:00:00';
 146  
 147      /**
 148       * The post's GMT modified time.
 149       *
 150       * @since 3.5.0
 151       * @var string
 152       */
 153      public $post_modified_gmt = '0000-00-00 00:00:00';
 154  
 155      /**
 156       * A utility DB field for post content.
 157       *
 158       * @since 3.5.0
 159       * @var string
 160       */
 161      public $post_content_filtered = '';
 162  
 163      /**
 164       * ID of a post's parent post.
 165       *
 166       * @since 3.5.0
 167       * @var int
 168       */
 169      public $post_parent = 0;
 170  
 171      /**
 172       * The unique identifier for a post, not necessarily a URL, used as the feed GUID.
 173       *
 174       * @since 3.5.0
 175       * @var string
 176       */
 177      public $guid = '';
 178  
 179      /**
 180       * A field used for ordering posts.
 181       *
 182       * @since 3.5.0
 183       * @var int
 184       */
 185      public $menu_order = 0;
 186  
 187      /**
 188       * The post's type, like post or page.
 189       *
 190       * @since 3.5.0
 191       * @var string
 192       */
 193      public $post_type = 'post';
 194  
 195      /**
 196       * An attachment's mime type.
 197       *
 198       * @since 3.5.0
 199       * @var string
 200       */
 201      public $post_mime_type = '';
 202  
 203      /**
 204       * Cached comment count.
 205       *
 206       * A numeric string, for compatibility reasons.
 207       *
 208       * @since 3.5.0
 209       * @var string
 210       * @phpstan-var numeric-string
 211       */
 212      public $comment_count = '0';
 213  
 214      /**
 215       * Stores the post object's sanitization level.
 216       *
 217       * Does not correspond to a DB field.
 218       *
 219       * @since 3.5.0
 220       * @var string
 221       */
 222      public $filter;
 223  
 224      /**
 225       * Retrieve WP_Post instance.
 226       *
 227       * @since 3.5.0
 228       *
 229       * @global wpdb $wpdb WordPress database abstraction object.
 230       *
 231       * @param int $post_id Post ID.
 232       * @return WP_Post|false Post object, false otherwise.
 233       */
 234  	public static function get_instance( $post_id ) {
 235          global $wpdb;
 236  
 237          $post_id = (int) $post_id;
 238          if ( $post_id <= 0 ) {
 239              return false;
 240          }
 241  
 242          $_post = wp_cache_get( $post_id, 'posts' );
 243  
 244          if ( ! $_post ) {
 245              $_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d LIMIT 1", $post_id ) );
 246  
 247              if ( ! $_post ) {
 248                  return false;
 249              }
 250  
 251              $_post = sanitize_post( $_post, 'raw' );
 252              wp_cache_add( $_post->ID, $_post, 'posts' );
 253          } elseif ( empty( $_post->filter ) || 'raw' !== $_post->filter ) {
 254              $_post = sanitize_post( $_post, 'raw' );
 255          }
 256  
 257          return new WP_Post( $_post );
 258      }
 259  
 260      /**
 261       * Constructor.
 262       *
 263       * @since 3.5.0
 264       *
 265       * @param object $post Post object.
 266       */
 267  	public function __construct( $post ) {
 268          foreach ( get_object_vars( $post ) as $key => $value ) {
 269              $this->$key = $value;
 270          }
 271      }
 272  
 273      /**
 274       * Isset-er.
 275       *
 276       * @since 3.5.0
 277       *
 278       * @param string $key Property to check if set.
 279       * @return bool
 280       */
 281  	public function __isset( $key ) {
 282          if ( 'ancestors' === $key ) {
 283              return true;
 284          }
 285  
 286          if ( 'page_template' === $key ) {
 287              return true;
 288          }
 289  
 290          if ( 'post_category' === $key ) {
 291              return true;
 292          }
 293  
 294          if ( 'tags_input' === $key ) {
 295              return true;
 296          }
 297  
 298          return metadata_exists( 'post', $this->ID, $key );
 299      }
 300  
 301      /**
 302       * Getter.
 303       *
 304       * @since 3.5.0
 305       *
 306       * @param string $key Key to get.
 307       * @return mixed
 308       */
 309  	public function __get( $key ) {
 310          if ( 'page_template' === $key && $this->__isset( $key ) ) {
 311              return get_post_meta( $this->ID, '_wp_page_template', true );
 312          }
 313  
 314          if ( 'post_category' === $key ) {
 315              if ( is_object_in_taxonomy( $this->post_type, 'category' ) ) {
 316                  $terms = get_the_terms( $this, 'category' );
 317              }
 318  
 319              if ( empty( $terms ) ) {
 320                  return array();
 321              }
 322  
 323              return wp_list_pluck( $terms, 'term_id' );
 324          }
 325  
 326          if ( 'tags_input' === $key ) {
 327              if ( is_object_in_taxonomy( $this->post_type, 'post_tag' ) ) {
 328                  $terms = get_the_terms( $this, 'post_tag' );
 329              }
 330  
 331              if ( empty( $terms ) ) {
 332                  return array();
 333              }
 334  
 335              return wp_list_pluck( $terms, 'name' );
 336          }
 337  
 338          // Rest of the values need filtering.
 339          if ( 'ancestors' === $key ) {
 340              $value = get_post_ancestors( $this );
 341          } else {
 342              $value = get_post_meta( $this->ID, $key, true );
 343          }
 344  
 345          if ( $this->filter ) {
 346              $value = sanitize_post_field( $key, $value, $this->ID, $this->filter );
 347          }
 348  
 349          return $value;
 350      }
 351  
 352      /**
 353       * {@Missing Summary}
 354       *
 355       * @since 3.5.0
 356       *
 357       * @param string $filter Filter.
 358       * @return WP_Post
 359       */
 360  	public function filter( $filter ) {
 361          if ( $this->filter === $filter ) {
 362              return $this;
 363          }
 364  
 365          if ( 'raw' === $filter ) {
 366              return self::get_instance( $this->ID );
 367          }
 368  
 369          return sanitize_post( $this, $filter );
 370      }
 371  
 372      /**
 373       * Convert object to array.
 374       *
 375       * @since 3.5.0
 376       *
 377       * @return array Object as array.
 378       */
 379  	public function to_array() {
 380          $post = get_object_vars( $this );
 381  
 382          foreach ( array( 'ancestors', 'page_template', 'post_category', 'tags_input' ) as $key ) {
 383              if ( $this->__isset( $key ) ) {
 384                  $post[ $key ] = $this->__get( $key );
 385              }
 386          }
 387  
 388          return $post;
 389      }
 390  }


Generated : Thu Jul 2 08:20:12 2026 Cross-referenced by PHPXref