diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index e48d942f0c421..1f6f926b1a05e 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -3963,6 +3963,9 @@ public function is_attachment( $attachment = '' ) { $attachment = array_map( 'strval', (array) $attachment ); $post_obj = $this->get_queried_object(); + if ( ! $post_obj ) { + return false; + } if ( in_array( (string) $post_obj->ID, $attachment, true ) ) { return true; @@ -3996,6 +3999,9 @@ public function is_author( $author = '' ) { } $author_obj = $this->get_queried_object(); + if ( ! $author_obj ) { + return false; + } $author = array_map( 'strval', (array) $author ); @@ -4032,6 +4038,9 @@ public function is_category( $category = '' ) { } $cat_obj = $this->get_queried_object(); + if ( ! $cat_obj ) { + return false; + } $category = array_map( 'strval', (array) $category ); @@ -4068,6 +4077,9 @@ public function is_tag( $tag = '' ) { } $tag_obj = $this->get_queried_object(); + if ( ! $tag_obj ) { + return false; + } $tag = array_map( 'strval', (array) $tag ); @@ -4315,6 +4327,9 @@ public function is_page( $page = '' ) { } $page_obj = $this->get_queried_object(); + if ( ! $page_obj ) { + return false; + } $page = array_map( 'strval', (array) $page ); @@ -4422,6 +4437,9 @@ public function is_single( $post = '' ) { } $post_obj = $this->get_queried_object(); + if ( ! $post_obj ) { + return false; + } $post = array_map( 'strval', (array) $post ); @@ -4469,6 +4487,9 @@ public function is_singular( $post_types = '' ) { } $post_obj = $this->get_queried_object(); + if ( ! $post_obj ) { + return false; + } return in_array( $post_obj->post_type, (array) $post_types, true ); } diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index a6162b8a0a0e2..5ef98e1f726ba 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -788,4 +788,113 @@ public function test_is_post_type_archive_should_return_false_for_an_undefined_p $this->assertFalse( is_post_type_archive( $post_type ) ); } + + /** + * @ticket 29660 + */ + public function test_query_singular_404_does_not_throw_warning() { + $q = new WP_Query( + array( + 'pagename' => 'non-existent-page', + ) + ); + + $this->assertSame( 0, $q->post_count ); + $this->assertFalse( $q->is_single() ); + + $this->assertTrue( $q->is_singular() ); + $this->assertFalse( $q->is_singular( 'page' ) ); + + $this->assertTrue( $q->is_page() ); + $this->assertFalse( $q->is_page( 'non-existent-page' ) ); + } + + /** + * @ticket 29660 + */ + public function test_query_single_404_does_not_throw_warning() { + $q = new WP_Query( + array( + 'name' => 'non-existent-post', + ) + ); + + $this->assertSame( 0, $q->post_count ); + $this->assertFalse( $q->is_page() ); + + $this->assertTrue( $q->is_singular() ); + $this->assertFalse( $q->is_singular( 'post' ) ); + + $this->assertTrue( $q->is_single() ); + $this->assertFalse( $q->is_single( 'non-existent-post' ) ); + } + + /** + * @ticket 29660 + */ + public function test_query_attachment_404_does_not_throw_warning() { + $q = new WP_Query( + array( + 'attachment' => 'non-existent-attachment', + ) + ); + + $this->assertSame( 0, $q->post_count ); + + $this->assertTrue( $q->is_singular() ); + $this->assertFalse( $q->is_singular( 'attachment' ) ); + + $this->assertTrue( $q->is_attachment() ); + $this->assertFalse( $q->is_attachment( 'non-existent-attachment' ) ); + } + + /** + * @ticket 29660 + */ + public function test_query_author_404_does_not_throw_warning() { + $q = new WP_Query( + array( + 'author_name' => 'non-existent-author', + ) + ); + + $this->assertSame( 0, $q->post_count ); + + $this->assertTrue( $q->is_author() ); + $this->assertFalse( $q->is_author( 'non-existent-author' ) ); + } + + /** + * @ticket 29660 + */ + public function test_query_category_404_does_not_throw_warning() { + $q = new WP_Query( + array( + 'category_name' => 'non-existent-category', + ) + ); + + $this->assertSame( 0, $q->post_count ); + + $this->assertTrue( $q->is_category() ); + $this->assertFalse( $q->is_tax() ); + $this->assertFalse( $q->is_category( 'non-existent-category' ) ); + } + + /** + * @ticket 29660 + */ + public function test_query_tag_404_does_not_throw_warning() { + $q = new WP_Query( + array( + 'tag' => 'non-existent-tag', + ) + ); + + $this->assertSame( 0, $q->post_count ); + + $this->assertTrue( $q->is_tag() ); + $this->assertFalse( $q->is_tax() ); + $this->assertFalse( $q->is_tag( 'non-existent-tag' ) ); + } }