get_post_status

函数
get_post_status ( $post = null )
参数
  • (int|WP_Post) $post Optional. Post ID or post object. Defaults to global $post.
    Required:
    Default: null
返回值
  • (string|false) Post status on success, false on failure.
定义位置
相关方法
get_post_statusesget_post_statesget_post_stati_post_statesget_post_status_object
引入
2.0.0
弃用
-

get_post_status: 这个函数根据作为参数的文章ID或对象来检索文章的状态。状态可以是”发布”,”草稿”,”待定”,”私有”,”垃圾”,或任何由网站定义的自定义文章状态。

根据文章的ID,检索文章的状态。

如果文章的ID是一个附件,那么将给出父文章的状态。

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 );
}

常见问题

FAQs
查看更多 >