Query: Prevent PHP notice when get_post_type_object() returns null in is_post_type_archive().

This changeset avoids potential PHP notices caused by `get_post_type_object()` returning `null` when called inside `is_post_type_archive()`.

Props sean212, costdev.
Fixes #56287.


git-svn-id: https://develop.svn.wordpress.org/trunk@54464 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras 2022-10-10 23:01:08 +00:00
parent ce7c0362bf
commit 5ef83659d0
2 changed files with 23 additions and 0 deletions

View File

@ -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 );
}

View File

@ -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 ) );
}
}