diff --git a/includes/classes/Feature/Autosuggest/Autosuggest.php b/includes/classes/Feature/Autosuggest/Autosuggest.php index 67323b17f5..8e1bb67fa5 100644 --- a/includes/classes/Feature/Autosuggest/Autosuggest.php +++ b/includes/classes/Feature/Autosuggest/Autosuggest.php @@ -552,13 +552,36 @@ public function generate_search_query() { add_filter( 'posts_pre_query', [ $features->get_registered_feature( $this->slug ), 'return_empty_posts' ], 100, 1 ); // after ES Query to ensure we are not falling back to DB in any case - $search = new \WP_Query( - [ - 'post_type' => $post_type, - 'post_status' => $post_status, - 's' => $placeholder, - 'ep_integrate' => true, - ] + new \WP_Query( + /** + * Filter WP Query args of the autosuggest query template. + * + * If you want to display 20 posts in autosuggest: + * + * ``` + * add_filter( + * 'ep_autosuggest_query_args', + * function( $args ) { + * $args['posts_per_page'] = 20; + * return $args; + * } + * ); + * ``` + * + * @since 4.4.0 + * @hook ep_autosuggest_query_args + * @param {array} $args Query args + * @return {array} New query args + */ + apply_filters( + 'ep_autosuggest_query_args', + [ + 'post_type' => $post_type, + 'post_status' => $post_status, + 's' => $placeholder, + 'ep_integrate' => true, + ] + ) ); remove_filter( 'posts_pre_query', [ $features->get_registered_feature( $this->slug ), 'return_empty_posts' ], 100 ); diff --git a/tests/php/features/TestAutosuggest.php b/tests/php/features/TestAutosuggest.php index fde00b7d75..0f2da6b517 100644 --- a/tests/php/features/TestAutosuggest.php +++ b/tests/php/features/TestAutosuggest.php @@ -195,6 +195,65 @@ public function testGenerateSearchQuery() { $this->assertContains( 'ep_autosuggest_placeholder', $query ); } + public function testGenerateSearchQueryFilters() { + /** + * Test the `ep_autosuggest_query_placeholder` filter. + */ + $test_placeholder_filter = function() { + return 'lorem-ipsum'; + }; + + add_filter( 'ep_autosuggest_query_placeholder', $test_placeholder_filter ); + + $query = $this->get_feature()->generate_search_query(); + $this->assertContains( 'lorem-ipsum', $query['body'] ); + + remove_filter( 'ep_autosuggest_query_placeholder', $test_placeholder_filter ); + + /** + * Test the `ep_autosuggest_query_placeholder` filter. + */ + $test_post_type_filter = function() { + return [ 'my-custom-post-type' ]; + }; + + add_filter( 'ep_term_suggest_post_type', $test_post_type_filter ); + + $query = $this->get_feature()->generate_search_query(); + $this->assertContains( 'my-custom-post-type', $query['body'] ); + + remove_filter( 'ep_term_suggest_post_type', $test_post_type_filter ); + + /** + * Test the `ep_term_suggest_post_status` filter. + */ + $test_post_status_filter = function() { + return [ 'trash' ]; + }; + + add_filter( 'ep_term_suggest_post_status', $test_post_status_filter ); + + $query = $this->get_feature()->generate_search_query(); + $this->assertContains( 'trash', $query['body'] ); + + remove_filter( 'ep_term_suggest_post_status', $test_post_status_filter ); + + /** + * Test the `ep_term_suggest_post_status` filter. + */ + $test_args_filter = function( $args ) { + $args['posts_per_page'] = 1234; + return $args; + }; + + add_filter( 'ep_autosuggest_query_args', $test_args_filter ); + + $query = $this->get_feature()->generate_search_query(); + $this->assertContains( '1234', $query['body'] ); + + remove_filter( 'ep_autosuggest_query_args', $test_args_filter ); + } + public function testReturnEmptyPosts() { $this->assertEmpty( $this->get_feature()->return_empty_posts() ); }