get_post_status

Retrieve the post status based on the Post ID.

Description

(string|false) get_post_status( (string) $ID = '' ); 

If the post ID is of an attachment, then the parent post status will be given instead.

Returns (string|false)

Post status on success, false on failure.

Parameters (1)

0. $id — Optional. (string)
Post ID or post object. Default empty.

Usage

  1. if ( !function_exists( 'get_post_status' ) ) { 
  2. require_once ABSPATH . WPINC . '/post.php'; 
  3.  
  4. // Optional. Post ID or post object. Default empty. 
  5. $id = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_post_status($id); 
  9.  

Defined (1)

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

/wp-includes/post.php  
  1. function get_post_status( $ID = '' ) { 
  2. $post = get_post($ID); 
  3.  
  4. if ( !is_object($post) ) 
  5. return false; 
  6.  
  7. if ( 'attachment' == $post->post_type ) { 
  8. if ( 'private' == $post->post_status ) 
  9. return 'private'; 
  10.  
  11. // Unattached attachments are assumed to be published. 
  12. if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent) ) 
  13. return 'publish'; 
  14.  
  15. // Inherit status from the parent. 
  16. if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) { 
  17. $parent_post_status = get_post_status( $post->post_parent ); 
  18. if ( 'trash' == $parent_post_status ) { 
  19. return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true ); 
  20. } else { 
  21. return $parent_post_status; 
  22.  
  23.  
  24. /** 
  25. * Filters the post status. 
  26. * @since 4.4.0 
  27. * @param string $post_status The post status. 
  28. * @param WP_Post $post The post object. 
  29. */ 
  30. return apply_filters( 'get_post_status', $post->post_status, $post );