diff --git a/includes/classes/Feature/Documents/Documents.php b/includes/classes/Feature/Documents/Documents.php index f5046b011d..f82e5e1587 100644 --- a/includes/classes/Feature/Documents/Documents.php +++ b/includes/classes/Feature/Documents/Documents.php @@ -129,6 +129,11 @@ public function setup_document_search( $query ) { return; } + // Bail, if query is for media library. + if ( isset( $_REQUEST['action'] ) && 'query-attachments' === $_REQUEST['action'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended + return; + } + $post_status = $query->get( 'post_status', [] ); $post_type = $query->get( 'post_type', [] ); $mime_types = $query->get( 'post_mime_type', [] ); diff --git a/tests/php/features/TestDocuments.php b/tests/php/features/TestDocuments.php index 59c15d772e..7a027bb4ca 100644 --- a/tests/php/features/TestDocuments.php +++ b/tests/php/features/TestDocuments.php @@ -107,8 +107,6 @@ public function testSearchDisallowedMimeType() { // Need to call this since it's hooked to init ElasticPress\Features::factory()->get_registered_feature( 'search' )->search_setup(); - $post_ids = array(); - $this->ep_factory->post->create(); $this->ep_factory->post->create( array( @@ -152,8 +150,6 @@ public function testSearchNormalPost() { // Need to call this since it's hooked to init ElasticPress\Features::factory()->get_registered_feature( 'search' )->search_setup(); - $post_ids = array(); - $this->ep_factory->post->create( array( 'post_content' => 'findme', @@ -226,4 +222,38 @@ public function testExcludeFromSearchQuery() { $this->assertTrue( $query->elasticsearch_success ); $this->assertEquals( 2, $query->post_count ); } + + /** + * Test that search in media library is working correctly. + * + * @since 5.1.0 + * @group documents + */ + public function testQueryForAttachments() { + ElasticPress\Features::factory()->activate_feature( 'search' ); + ElasticPress\Features::factory()->activate_feature( 'documents' ); + ElasticPress\Features::factory()->setup_features(); + + $this->ep_factory->post->create( + array( + 'post_content' => 'search me', + 'post_type' => 'attachment', + 'post_mime_type' => 'image', + ) + ); + + ElasticPress\Elasticsearch::factory()->refresh_indices(); + + $_REQUEST['action'] = 'query-attachments'; + $args = array( + 's' => 'search me', + 'post_type' => 'attachment', + 'post_status' => 'inherit', + ); + + $query = new \WP_Query( $args ); + + $this->assertTrue( $query->elasticsearch_success ); + $this->assertEquals( 1, $query->post_count ); + } }