-
Type:
Task
-
Resolution: Merged
-
Priority:
Should have
-
None
-
None
-
3
-
Search, Technical Debt
-
Sprint #233, Sprint #234
-
umbriel
Summary
The way search currently works generates 2 queries to ElasticSearch for 1 search.
- The Main Query, defined by WordPress depending on the type of request, always runs (unless explicitely bypassed)
- Elasticpress already integrates automatically with the main query when it is a search query
- We create a new query to execute the search
We could rewrite our search to use and integrate more cleanly with the main query, using 'pre_get_posts' hook for example, and set parameters on it. Example:
add_action('pre_get_posts', function(\WP_Query $query) { if (!is_search() || !$query->is_search()) { return; } $query->set('ep_integrate', true); $query->set('posts_per_page', Search::POSTS_PER_LOAD); $query->set('post_type', Search::get_post_types()); $query->set('post_status', ['publish', 'inherit']); $query->set('has_password', false); $query->set('search_fields', [ 'post_title', 'post_content', 'post_excerpt', 'post_author.display_name', ]); $query->set('meta_query', [ 'key' => 'p4_do_not_index', 'compare' => 'NOT EXISTS', ]); }, 10, 1);
Alternatively, we can try to block the main query in that situation, maybe using 'do_parse_request' hook.