get_post_status

The timeline below displays how wordpress function get_post_status has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Retrieves the post status based on the post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
 * @return string|false Post status on success, false on failure.
 */
function get_post_status($post = null)
{
    $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);
}

WordPress Version: 5.9

/**
 * Retrieve the post status based on the post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.
 * @return string|false Post status on success, false on failure.
 */
function get_post_status($post = null)
{
    $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);
}

WordPress Version: 5.7

/**
 * Retrieve the post status based on the post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post..
 * @return string|false Post status on success, false on failure.
 */
function get_post_status($post = null)
{
    $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);
}

WordPress Version: 5.5

/**
 * Retrieve the post status based on the post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post..
 * @return string|false Post status on success, false on failure.
 */
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);
}

WordPress Version: 5.1

/**
 * Retrieve the post status based on the post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post..
 * @return string|false Post status on success, false on failure.
 */
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);
}

WordPress Version: 4.6

/**
 * Retrieve the post status based on the Post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
 * @return string|false Post status on success, false on failure.
 */
function get_post_status($ID = '')
{
    $post = get_post($ID);
    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);
}

WordPress Version: 4.4

/**
 * Retrieve the post status based on the Post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
 * @return string|false Post status on success, false on failure.
 */
function get_post_status($ID = '')
{
    $post = get_post($ID);
    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;
            }
        }
    }
    /**
     * Filter 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);
}

WordPress Version: 4.1

/**
 * Retrieve the post status based on the Post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
 * @return string|false Post status on success, false on failure.
 */
function get_post_status($ID = '')
{
    $post = get_post($ID);
    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;
            }
        }
    }
    return $post->post_status;
}

WordPress Version: 4.0

/**
 * Retrieve the post status based on the Post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $ID Optional. Post ID or post object. Default empty.
 * @return string|bool Post status on success, false on failure.
 */
function get_post_status($ID = '')
{
    $post = get_post($ID);
    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;
            }
        }
    }
    return $post->post_status;
}

WordPress Version: 3.9

/**
 * Retrieve the post status based on the Post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int|WP_Post $ID Optional. Post ID or post object.
 * @return string|bool Post status on success, false on failure.
 */
function get_post_status($ID = '')
{
    $post = get_post($ID);
    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) {
            return get_post_status($post->post_parent);
        }
    }
    return $post->post_status;
}

WordPress Version: 3.7

/**
 * Retrieve the post status based on the Post ID.
 *
 * If the post ID is of an attachment, then the parent post status will be given
 * instead.
 *
 * @since 2.0.0
 *
 * @param int $ID Optional. Post ID. Default is the current post from the loop.
 * @return string|bool Post status on success, false on failure.
 */
function get_post_status($ID = '')
{
    $post = get_post($ID);
    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) {
            return get_post_status($post->post_parent);
        }
    }
    return $post->post_status;
}