get_post_status()WP 2.0.0

Gets the status of the specified post: publish, draft, etc.

If the function was passed the ID of an attachment (attached post), the function will return the status of the parent post. If the attachment is not attached to any post, it will be considered published - the function will return publish.

List of basic statuses in WordPress:

publish
future
draft
pending
private
trash
auto-draft
inherit

Returns

String|false. The status of the post on success, false if it failed to get the status.

1 time — 0.000357 sec (fast) | 50000 times — 0.29 sec (very fast) | PHP 7.0.5, WP 4.4.2
Hooks from the function

Usage

get_post_status( $post );
$post(int/WP_Post)
Identifier (ID) of the post or post object WP_Post.
Default: null (current post in the loop)

Examples

0

#1 Display the status of the post with ID 121

$status = get_post_status( 121 );

echo $status;

/* Output:

'publish' - if the post is published.
'draft' - if it is a draft, etc.

*/
0

#2 Display the localized name of the post status

$status = get_post_status( 121 );
echo get_post_status_object( $status )->label; // Published
0

#3 Return one of:

publish, future, draft, pending, private, trash, auto-draft, inherit, custom status.

Description for each status See here.

0

#4 Check post status

Code for using outside of The Loop:

if ( 'private' === get_post_status( $post_id ) ) {
	echo 'private';
} 
else {
	echo 'public';
}

Changelog

Since 2.0.0 Introduced.

get_post_status() code WP 6.9.1

function get_post_status( $post = null ) {
	// Normalize the post object if necessary, skip normalization if called from get_sample_permalink().
	if ( ! $post instanceof WP_Post || ! isset( $post->filter ) || 'sample' !== $post->filter ) {
		$post = get_post( $post );
	}

	if ( ! is_object( $post ) ) {
		return false;
	}

	$post_status = $post->post_status;

	if (
		'attachment' === $post->post_type &&
		'inherit' === $post_status
	) {
		if (
			0 === $post->post_parent ||
			! get_post( $post->post_parent ) ||
			$post->ID === $post->post_parent
		) {
			// Unattached attachments with inherit status are assumed to be published.
			$post_status = 'publish';
		} elseif ( 'trash' === get_post_status( $post->post_parent ) ) {
			// Get parent status prior to trashing.
			$post_status = get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );

			if ( ! $post_status ) {
				// Assume publish as above.
				$post_status = 'publish';
			}
		} else {
			$post_status = get_post_status( $post->post_parent );
		}
	} elseif (
		'attachment' === $post->post_type &&
		! in_array( $post_status, array( 'private', 'trash', 'auto-draft' ), true )
	) {
		/*
		 * Ensure uninherited attachments have a permitted status either 'private', 'trash', 'auto-draft'.
		 * This is to match the logic in wp_insert_post().
		 *
		 * Note: 'inherit' is excluded from this check as it is resolved to the parent post's
		 * status in the logic block above.
		 */
		$post_status = 'publish';
	}

	/**
	 * Filters the post status.
	 *
	 * @since 4.4.0
	 * @since 5.7.0 The attachment post type is now passed through this filter.
	 *
	 * @param string  $post_status The post status.
	 * @param WP_Post $post        The post object.
	 */
	return apply_filters( 'get_post_status', $post_status, $post );
}