get_post_status()

You are here:

get_post_status( int|WP_Post $post = null )

Retrieve the post status based on the post ID.

Description Description

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


Top ↑

Parameters Parameters

$post

(int|WP_Post) (Optional) Post ID or post object. Defaults to global $post..

Default value: null


Top ↑

Return Return

(string|false) Post status on success, false on failure.


Top ↑

Source Source

File: wp-includes/post.php

897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
function get_post_status( $post = null ) {
    $post = get_post( $post );
 
    if ( ! is_object( $post ) ) {
        return false;
    }
 
    if ( 'attachment' === $post->post_type ) {
        if ( 'private' === $post->post_status ) {
            return 'private';
        }
 
        // Unattached attachments are assumed to be published.
        if ( ( 'inherit' === $post->post_status ) && ( 0 == $post->post_parent ) ) {
            return 'publish';
        }
 
        // Inherit status from the parent.
        if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) {
            $parent_post_status = get_post_status( $post->post_parent );
            if ( 'trash' === $parent_post_status ) {
                return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true );
            } else {
                return $parent_post_status;
            }
        }
    }
 
    /**
     * Filters the post status.
     *
     * @since 4.4.0
     *
     * @param string  $post_status The post status.
     * @param WP_Post $post        The post object.
     */
    return apply_filters( 'get_post_status', $post->post_status, $post );
}


Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.
Was this article helpful?
Dislike 0
Views: 13