Skip to content

Enterprise Search

Search and multisite

After completing the steps to code-enable Enterprise Search, indexes can be created for subsites individually or network-wide on a WordPress multisite.

VIP-CLI command examples

For demonstration purposes, the <app-alias> value mytestsite 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.

Setting up indexes for all subsites

To avoid having some subsites with non-functional searches, set up indexes for all subsites on a WordPress multisite at the same time by including the --network-wide option.

vip @mytestsite.develop -- wp vip-search index --setup --network-wide

Caution

If a multisite has over 100 subsites, 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 subsites that will actively use them. Creating unused indexes may negatively impact the indexing and revalidation operations triggered by periodic health checks.

Creating an index for a new subsite

By default, an index is not automatically created for a newly added subsite even if Enterprise Search is already enabled for the multisite.

When a new subsite is created on a multisite, create a new index for that subsite with the wp vip-search index --setup command. Target the specific subsite by passing the value of the subsite’s domain with the --url option:

vip @mytestsite.develop -- wp vip-search index --setup --url=mysubsite.example.com

If a subsite has been updated by a SQL database import, the wp vip-search index --setup command should be used to re-index the subsite and bring the database and Elasticsearch index back into sync.

A newly created or updated index for a subsite can be tested (and any existing issues identified) by using Search Dev Tools or the wp vip-search health commands.

Searching across subsites

To enable search functionality across subsites, define the EP_IS_NETWORK constant as true in vip-config.php.

define( 'EP_IS_NETWORK', true );

To search across subsites, use the sites parameter in WP_Query. The default value is at current, but other accepted values are a subsite ID, all, or an array of subsite IDs.

For example, to enable searching across subsites 2 and 3, use the pre_get_posts hook:

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

Alternatively, use WP_Query to search for the keyword “test” across subsites 2 and 3, set the parameter ep_integrate to true:

$query = new WP_Query(
	array(
		's' => 'test',
		'sites' => array( 2, 3 ), // Search only in subsites 2 and 3 
		'ep_integrate' => true,
	)
);

Using the all parameter in WP_Query requires that:

  • All subsites on the network have been set up with an index with correct mappings.
  • An alias index (which points to every index in the network) has been created with the VIP-CLI command vip-search recreate-network-alias:
vip @mytestsite.develop -- wp vip-search recreate-network-alias

Note

When a subsite is created and its index has been set up with the correct mapping, the VIP-CLI command vip-search recreate-network-alias will need to be run again to account for the new subsite.

Last updated: April 11, 2022