SearchWP

Multisite (Network, MU, MultiUser) Search

SearchWP is able to perform cross-site Multisite/Network searches. There are a number of things to mention/consider regarding the implementation.

Note: There are important limitations to be aware of!

Abstract

Cross-site searches are possible in SearchWP. Any Engine from any site can be used for a cross-site search.

Note: SearchWP’s Engines control what is indexed on each sub-site. If the Engine you are using to perform the search has different Sources/Attributes/Rules than the Engine(s) on the sub-sites you are searching the results may not be accurate.

For example: if Posts have been added to the Engine you are using for the search, but a sub-site does not have an Engine with Posts enabled, that sub-site will not return Posts.

For a comprehensive cross-site search, ensure that all sites share a similar configuration and applicable Engine.

Implementation Details

In a Multisite environment, SearchWP builds a comprehensive search index for the entirety of the network. By default SearchWP will limit results to Entries of the current site.

You can utilize \SearchWP\Query to execute searches across multiple sites within the network by customizing the site parameter to contain either:

  • Array of site IDs (or a comma separated string of site IDs)
  • 'all' to search all sites on the network

Important: the primary concept to understand is that SearchWP’s Engines determine what content is indexed for all sites within a network.

This is significant because \SearchWP\Query uses an Engine from the current site to perform the search. Let’s say the Default Engine of the current site (SITE A) has the following Sources and nothing else:

  • Posts
  • Pages

Within this example network there is a second site (SITE B) that has a Default Engine with the following Sources and nothing else:

  • Posts

If you are performing a \SearchWP\Query in SITE A using its Default Engine, Pages from SITE B will not be returned because Pages were not added to the Default Engine of SITE B.

This may seem counter-intuitive because Pages are added to the Default Engine of SITE A, but the Engines of SITE B define and control what is indexed for that site.

Recommended Engine configuration

Given the above, you can utilize a Supplemental Engine on each network site that shares a common configuration.

Alternatively, you can ensure that your network is set up in such a way that the Engine used for a \SearchWP\Query is the least common denominator for all sites within the network.

Handling Multisite search results

\SearchWP\Query allows you to specify how results are returned using the fields parameter.

By default results are returned as an array of objects each with the following properties: 'id', 'source', 'site', 'relevance'

This format is most useful for Multisite search results because it gives you the ability to switch_to_blog() when necessary so as to retrieve accurate details for each result.

If however you choose to use another supported format it may not include critical site ID information so please keep that in mind as you structure your Multisite searches.

Note: This applies specifically when setting fields to 'all' which will return each result in its native format (e.g. as \WP_Post). Any assumptions made in your results output (e.g. use of get_permalink()) are based on the current site, not necessarily the site from which the result was returned!

Given that, it is always recommended to use the default value for fields and manually switch_to_blog() (and subsequently restore_current_blog()) when necessary!