diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index bb7e37a9a2..cccc5c0871 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -1665,12 +1665,20 @@ function set_post_type( $post_id = 0, $post_type = 'post' ) { * For all others, the 'publicly_queryable' value will be used. * * @since 4.4.0 + * @since 4.5.0 Added the ability to pass a post type name in addition to object. * - * @param object $post_type_object Post type object. + * @param object $post_type Post type name or object. * @return bool Whether the post type should be considered viewable. */ -function is_post_type_viewable( $post_type_object ) { - return $post_type_object->publicly_queryable || ( $post_type_object->_builtin && $post_type_object->public ); +function is_post_type_viewable( $post_type ) { + if ( is_scalar( $post_type ) ) { + $post_type = get_post_type_object( $post_type ); + if ( ! $post_type ) { + return false; + } + } + + return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public ); } /** diff --git a/tests/phpunit/tests/post/isPostTypeViewable.php b/tests/phpunit/tests/post/isPostTypeViewable.php index 17b3f7de7b..2a3956bbc1 100644 --- a/tests/phpunit/tests/post/isPostTypeViewable.php +++ b/tests/phpunit/tests/post/isPostTypeViewable.php @@ -71,4 +71,24 @@ class Tests_Post_IsPostTypeViewable extends WP_UnitTestCase { $page = get_post_type_object( 'page' ); $this->assertTrue( is_post_type_viewable( $page ) ); } + + /** + * @ticket 35609 + */ + public function test_should_accept_post_type_name() { + register_post_type( 'wptests_pt', array( + 'publicly_queryable' => true, + '_builtin' => false, + 'public' => false, + ) ); + + $this->assertTrue( is_post_type_viewable( 'wptests_pt' ) ); + } + + /** + * @ticket 35609 + */ + public function test_should_return_false_for_bad_post_type_name() { + $this->assertFalse( is_post_type_viewable( 'foo' ) ); + } }