diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 8525987454..e48d942f0c 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3935,6 +3935,10 @@ class WP_Query { } $post_type_object = get_post_type_object( $post_type ); + if ( ! $post_type_object ) { + return false; + } + return in_array( $post_type_object->name, (array) $post_types, true ); } diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index fef3e19748..a6162b8a0a 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -769,4 +769,23 @@ class Tests_Query extends WP_UnitTestCase { $this->assertArrayHasKey( 'join', $posts_clauses_request ); $this->assertSame( '/* posts_join_request */', $posts_clauses_request['join'] ); } + + /** + * Tests that is_post_type_archive() returns false for an undefined post type. + * + * @ticket 56287 + * + * @covers ::is_post_type_archive + */ + public function test_is_post_type_archive_should_return_false_for_an_undefined_post_type() { + global $wp_query; + + $post_type = '56287-post-type'; + + // Force the request to be a post type archive. + $wp_query->is_post_type_archive = true; + $wp_query->set( 'post_type', $post_type ); + + $this->assertFalse( is_post_type_archive( $post_type ) ); + } }