Skip to content

Commit 9c72fd7

Browse files
Posts/Post Types: Add filter to is_post_type_viewable().
Introduces a new filter `'is_post_type_viewable'` which allows overriding the check. The expected filtered value is a boolean. As filtered values can change, including the data type, this commit includes a `is_bool()` check, thus ensuring backwards-compatibility. Follow-up to [33666], [36402]. Props audrasjb, deepaklalwani, hellofromTonya, peterwilsoncc, powerbuoy, sergeybiryukov. Fixes #49628. git-svn-id: https://develop.svn.wordpress.org/trunk@52024 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c3d8812 commit 9c72fd7

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

src/wp-includes/post.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,7 @@ function set_post_type( $post_id = 0, $post_type = 'post' ) {
20862086
* @since 4.4.0
20872087
* @since 4.5.0 Added the ability to pass a post type name in addition to object.
20882088
* @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object.
2089+
* @since 5.9.0 Added `is_post_type_viewable` hook to filter the result.
20892090
*
20902091
* @param string|WP_Post_Type $post_type Post type name or object.
20912092
* @return bool Whether the post type should be considered viewable.
@@ -2102,7 +2103,24 @@ function is_post_type_viewable( $post_type ) {
21022103
return false;
21032104
}
21042105

2105-
return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );
2106+
$is_viewable = $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public );
2107+
2108+
/**
2109+
* Filters whether a post type is considered "viewable".
2110+
*
2111+
* @since 5.9.0
2112+
*
2113+
* @param bool $is_viewable Whether the post type is "viewable".
2114+
* @param WP_Post_Type $post_type Post type object.
2115+
*/
2116+
$is_viewable = apply_filters( 'is_post_type_viewable', $is_viewable, $post_type );
2117+
2118+
// Make sure the filtered value is a boolean type before returning it.
2119+
if ( ! is_bool( $is_viewable ) ) {
2120+
return false;
2121+
}
2122+
2123+
return $is_viewable;
21062124
}
21072125

21082126
/**

0 commit comments

Comments
 (0)