函数文档

is_post_publicly_viewable()

💡 云策文档标注

概述

is_post_publicly_viewable() 函数用于判断文章是否可公开查看。它基于文章类型和文章状态的可查看性进行综合评估。

关键要点

  • 函数检查文章类型和文章状态是否均可查看,使用 is_post_type_viewable() 和 is_post_status_viewable() 函数。
  • 参数 $post 可选,可以是文章 ID、WP_Post 对象或 null(默认使用全局 $post)。
  • 返回布尔值,表示文章是否可公开查看。
  • 函数在 WordPress 5.7.0 版本中引入。

代码示例

function is_post_publicly_viewable( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$post_type   = get_post_type( $post );
	$post_status = get_post_status( $post );

	return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status );
}

📄 原文内容

Determines whether a post is publicly viewable.

Description

Posts are considered publicly viewable if both the post status and post type are viewable.

Parameters

$postint|WP_Post|nulloptional
Post ID or post object. Defaults to global $post.

Default:null

Return

bool Whether the post is publicly viewable.

Source

function is_post_publicly_viewable( $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$post_type   = get_post_type( $post );
	$post_status = get_post_status( $post );

	return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status );
}

Changelog

Version Description
5.7.0 Introduced.