Title: Enterprise Search on multisite
Author: WordPress VIP Documentation
Published: May 14, 2021
Last modified: May 26, 2025

---

 1. [Enterprise Search](https://docs.wpvip.com/enterprise-search/)
 2. Enterprise Search on multisite

#  Enterprise Search on multisite

After completing the steps to [code-enable Enterprise Search](https://docs.wpvip.com/vip-search/enable/)
for an environment, an Elasticsearch (ES) index must be created for each network
site that will be using Enterprise Search for search queries.

When new sites are added to the multisite network, an index will not be automatically
created for them.

**VIP-CLI command examples**

For demonstration purposes, the `<app-name>` value `example-app` and the `<env>`
value `develop` are used in the VIP-CLI command examples below. Read more about 
[how to target environments in VIP-CLI commands](https://docs.wpvip.com/vip-cli/target-environments/).

## Create an index for a network site

Create an index for a network site with the `wp vip-search index --setup` command.
Target the specific network site by passing the value of the network site’s ID with
the `--blog-ids` option.

In this command example, an index is created for network site ID `2`:

    ```wp-block-preformatted
    vip @example-app.develop -- wp vip-search index --setup --blog-ids=2
    ```

Use [Search Dev Tools](https://docs.wpvip.com/enterprise-search/dev-tools/) or the
[`wp vip-search health` commands](https://docs.wpvip.com/vip-search/check-index-health/)
to check that the new index is working as expected.

After an index has been successfully created for the network site, it can be targeted
in `wp vip-search` commands by passing the site address URL in the `--url` argument.
If the `--url` argument is omitted, the CLI command will target the index of the
main site (ID 1).

If a network site has been updated by a [SQL database import](https://docs.wpvip.com/databases/import/),
use [versioning to re-index the site](https://docs.wpvip.com/enterprise-search/version/)
and bring the database and Elasticsearch index back into sync.

## Search across network sites

WordPress multisite environments that have Enterprise Search enabled and indexes
created for more than one network site can configure search queries to run against
multiple indexes.

To configure search to run across more than one site index on a multisite network:

 1. Define the `EP_IS_NETWORK` constant as `true` in [`vip-config.php`](https://docs.wpvip.com/wordpress-skeleton/vip-config-directory/).

vip-config.php

    ```lang-php
    define( 'EP_IS_NETWORK', true );
    ```

 2. Create a “network alias index” for all site indexes that currently exist on the
    multisite network with the command `wp vip-search recreate-network-alias`. This
    command must be repeated to update the network alias index each time a new index
    is created for a site on the network.

    ```wp-block-preformatted
    vip @example-app.develop -- wp vip-search recreate-network-alias
    ```

 3. Using [the `pre_get_posts` method](https://developer.wordpress.org/reference/hooks/pre_get_posts/),
    specify which site indexes on the network will handle search queries with the `
    sites` parameter in `WP_Query`. The accepted values for the `sites` parameter are`
    current` (default), a site ID, an array of site IDs, or `all` (all site indexes
    that currently exist in the “network alias index”).

In this code example, the custom function `vip_search_across_sites()` modifies frontend
search queries to run across network site ID `2` and network site ID `3`:

    ```lang-php
    add_action( 'pre_get_posts', 'vip_search_across_sites' );
    function vip_search_across_sites( $query ) {
    	if ( $query->is_search() && $query->is_main_query() ) {
    		$query->set( 'sites', array( 2, 3 ) );
    	}
    }
    ```

Alternatively, customizations can be added to the cross-site search configuration
via `WP_Query`. In this code example, a query will perform a search for the keyword“
carrot” across network site ID `2` and network site ID `3`:

    ```lang-php
    $query = new WP_Query(
    	array(
    		's' => 'carrot',
    		'sites' => array( 2, 3 ), // Search only in site IDs 2 and 3 
    		'ep_integrate' => true,
    	)
    );
    ```

## Create indexes for all sites on a network

Indexes for all network sites on a multisite environment can be created simultaneously
by including the `--network-wide` option when running the `wp vip-search index --
setup` command.

For example:

    ```wp-block-preformatted
    vip @example-app.develop -- wp vip-search index --setup --network-wide
    ```

This method should only be used if Enterprise Search is necessary and will be actively
used by all sites on the network. Creating unused indexes can negatively impact 
the indexing and revalidation operations triggered by periodic health checks.

**Caution**

If a multisite has over 100 sites on the network, it is strongly recommend to create
an index **on a per-site basis**. Rather than creating indexes for _all_ sites, 
only create indexes for specific sites that will actively use them.

Last updated: May 26, 2025