Title: Offload a WP_Query to Enterprise Search
Author: WordPress VIP Documentation
Published: March 30, 2021
Last modified: February 18, 2026

---

 1. [Enterprise Search](https://docs.wpvip.com/enterprise-search/)
 2. Offload a WP_Query to Enterprise Search

#  Offload a WP_Query to Enterprise Search

When the Enterprise Search [Integration](https://docs.wpvip.com/integrations/) is
activated for a site, all standard front-end search queries passed with the `s` 
argument (e.g. `/?s=`) are automatically sent to Elasticsearch.

**By default, queries that are _not_ passed as a search argument will _not_ offload
to Elasticsearch**. Enabling non-standard queries to be offloaded requires a `WP_Query`
argument to be added.

**Note**

**Not all queries should be offloaded to Enterprise Search.** Unnecessarily offloading
queries can have unexpected negative effects. For example, if every request for 
a single post is offloaded to Enterprise Search it could result in [rate limiting](https://docs.wpvip.com/enterprise-search/rate-limiting/).

Simple queries that run very fast and do not show evidence of performance issues,
can continue to be handled by MySQL.

To test if a query works well without Elasticsearch, try running the query with 
and without the `es` parameter. Monitor and compare the speed of the query in [New Relic](https://docs.wpvip.com/performance/new-relic/)
and [Query Monitor](https://docs.wpvip.com/performance/query-monitor/). Based on
the performance observations, offload the identified slower queries to Enterprise
Search..

Slow queries and queries that are poorly performing (i.e. taxonomy and meta queries)
can also be identified in [the Slow Query Logs panel in the VIP Dashboard](https://docs.wpvip.com/logs/slow-query/).

## Adding the `es` parameter

The`es` parameter can be used to offload existing MySQL queries to Elasticsearch.
When the `es` parameter is set to `true`, the **es-wp-query** adapter is leveraged
instead of ElasticPress and effectively maps most SQL queries to Elasticsearch.

The built-in **es-wp-query** adapter can handle transforming a query into an Elasticsearch
request, obtain the search results (generally, a list of Post IDs), and then obtain
the post data from the database.

When working with the `es` parameter, each query should be tested individually to
ensure that it is returning the expected results. Adjustments can be made to the
query as needed.

**Caution**

The [facets feature](https://docs.wpvip.com/enterprise-search/enable-features/) 
in Enterprise Search _will not work_ without ElasticPress, and will fail if `es`
is set to `true` for search queries.

## Using `pre_get_posts`

When using `pre_get_posts`, it is important to [target the correct query](https://developer.wordpress.org/reference/hooks/pre_get_posts/#targeting-the-right-query).

In this code example, queries are offloaded using [the `pre_get_posts` hook](https://developer.wordpress.org/reference/hooks/pre_get_posts/):

    ```lang-php
    function vip_sample_es_offload( $query ) {
        if ( $query->is_main_query() && ! $query->is_category() ) { // Conditions to offload to ES.
            $query->set( 'es', true );
        }
    }
    add_action( 'pre_get_posts', 'vip_sample_es_offload' );
    ```

The `pre_get_posts` hook can also be used to selectively [offload queries in the WordPress Admin](https://docs.wpvip.com/enterprise-search/enable-for-wp-admin/).

## `WP_Query` arguments

The query argument can be added to the argument list where `WP_Query` (or `get_posts`)
is already being called by adding `'es' => true` to the query args:

    ```lang-php
    $query = new WP_Query(
    	array(
    		'post_type'      => 'post',
    		'post_status'    => 'publish',
    		'posts_per_page' => 10,
    		'es'             => true,
    	)
    );
    ```

If any of the queries are filtering by terms (or taxonomy, e.g. categories), the
[**terms** feature](https://docs.wpvip.com/enterprise-search/enable-features/) will
need to be enabled.

The [meta](https://docs.wpvip.com/vip-search/post-meta/) and [taxonomy](https://docs.wpvip.com/vip-search/post-taxonomies/)
filters are not indexed by default and should be reviewed as well.

## Advanced usage

### AJAX queries

To offload any `WP_Query` called by AJAX, use the `ep_ajax_wp_query_integration`
filter:

    ```lang-php
    add_filter( 'ep_ajax_wp_query_integration', '__return_true' );
    ```

The `ep_ajax_wp_query_integration` filter can also be used to [enable Enterprise Search to perform Media Library searches](https://docs.wpvip.com/vip-search/enable-for-wp-admin/)
in the WordPress Admin.

### Disable a fallback to MySQL queries

By default, any unsuccessful search query to an Elasticsearch index will fall back
to a regular MySQL database query. In some cases, this can cause degraded performance
of the database. This is particularly true if the queries that are intended to be
handled by Enterprise Search are complex and expensive.

A search query to an Elasticsearch index could be unsuccessful if:

 * It attempts to retrieve more than `10000` documents from an Elasticsearch index
 * Elasticsearch fails to parse the query.
 * It takes more than 2 seconds to complete.

To protect the performance of the database It may be beneficial to prevent queries
from falling back to MySQL at all.

In this code example a custom function utilizing the ElasticPress [`posts_pre_query`](https://developer.wordpress.org/reference/hooks/posts_pre_query/)
filter prevents the MySQL query fallback by returning `0` posts if an Elasticsearch
query has failed.

    ```lang-php
    add_filter( 'posts_pre_query', 'prevent_mysql_request_after_es_failed', 999, 2 );
    function prevent_mysql_request_after_es_failed( $posts, WP_Query $query ) {
        // If the ES query failed, return an empty array to prevent a MySQL query.
        // Adjust these conditions to match your needs.
        if ( isset( $query->elasticsearch_success ) && false === $query->elasticsearch_success ) {
            return [];
        }

        return $posts;
    }
    ```

### ElasticPress offloading via `ep_integrate`

If neither the `es` option nor the direct MySQL options produce the required search
results or speed, `ep_integrate` is an ElasticPress method that can be considered
for offloading select queries. `ep_integrate` can also be used for [searching across sites on a network](https://docs.wpvip.com/vip-search/search-and-multisite/#searching-across-subsites).

To use this method, add `ep_integrate` (set to `true`) instead of `es` in the **
WP_Query** arguments. This can be implemented in a case-by-case basis. For example,
almost all offloaded queries might use `es`, and one very specialized query might
use `ep_integrate`.

Review the [ElasticPress documentation](https://github.com/Automattic/ElasticPress/blob/master/docs/indexables.md)
before using this option. If the query involves terms or users, those [features will need to be enabled](https://docs.wpvip.com/vip-search/vip-search-features#h-terms).

**Note**

Queries that are offloaded with `ep_integrate` bypass the provided `es-wp-query`
adapter and use ElasticPress code instead. This may result in some special characters(
e.g. apostrophes, quotes) being filtered and normalized in a different way. In addition,
offloading queries with `ep_integrate` can produce excessive data that may negatively
affect site performance.

`ep_integrate` should be used sparingly, and its effects on site performance should
be tested thoroughly on a non-production environment before adding it to a production
environment.

Last updated: February 18, 2026