Within your WordPress plugin or theme, you may need to determine if a post is published, a draft, or some other status. Using the get_post_status() WordPress function will allow you to do exactly that. In this article, we will show you how to use the get_post_status() WordPress function.
If you would like more information on creating WordPress plugins, see our article Create a WordPress Plugin: A Tutorial.
Basic usage
<?php get_post_status( $ID ) ?>
Optional parameters
$ID: This is the only parameter that can be defined in which you can define the ID of the post that you want to get the status of. If left undefined, it will simply get the status of the current post.
Possible post statuses
The following are the possible statuses that can be returned from the get_post_status() function:
- publish: Published posts or pages.
- pending: Any post that is pending review or an editor or administrator.
- draft: A post that has been saved as a draft.
- auto-draft: This is a post that has been created but does not yet have any content.
- future: A post that has a future publish date set.
- private: This status is assigned to posts that are published but are only available to users who are logged in.
- inherit: This is assigned as a revision of another post.
- trash: The post has been placed in the trash.
Basic code example
<?php
$mypoststatus = get_post_status( 5 );
echo $mypoststatus;
?>
In this example, we are simply getting the post status of the post with ID 5 and assigning it to the $mypoststatus variable. Then using the echo command to display the contents of that variable which is the status of our post.
References
For more information on the get_post_status() function, you may take a look at the get_post_status WordPress Codex page.