钩子文档

is_post_type_viewable

💡 云策文档标注

概述

is_post_type_viewable 是一个 WordPress 过滤器,用于控制 post type 是否被视为“可查看”。它要求返回布尔值以确保类型安全,特别是在 PHP 8.1+ 中避免类型错误。

关键要点

  • 过滤器名称:is_post_type_viewable
  • 参数:$is_viewable(布尔值,严格类型)和 $post_type(WP_Post_Type 对象)
  • 返回值:必须为布尔类型,非布尔值将导致函数返回 false
  • 设计目的:保持向后兼容性,防止 PHP 8.1+ 中的潜在类型错误
  • 相关函数:is_post_type_viewable() 在 wp-includes/post.php 中定义
  • 引入版本:5.9.0

📄 原文内容

Filters whether a post type is considered “viewable”.

Description

The returned filtered value must be a boolean type to ensure is_post_type_viewable() only returns a boolean. This strictness is by design to maintain backwards-compatibility and guard against potential type errors in PHP 8.1+. Non-boolean values (even falsey and truthy values) will result in the function returning false.

Parameters

$is_viewablebool
Whether the post type is “viewable” (strict type).
$post_typeWP_Post_Type
Post type object.

Source

return true === apply_filters( 'is_post_type_viewable', $is_viewable, $post_type );

Changelog

Version Description
5.9.0 Introduced.