diff --git src/wp-admin/includes/plugin.php src/wp-admin/includes/plugin.php
index f54717f804..2db58a7c15 100644
--- src/wp-admin/includes/plugin.php
+++ src/wp-admin/includes/plugin.php
@@ -1096,19 +1096,31 @@ function validate_plugin_requirements( $plugin ) {
 	$plugin_data = array_merge( $plugin_data, get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin ) );
 
 	if ( ! $plugin_data['wp_compatible'] && ! $plugin_data['php_compatible'] ) {
-		return new WP_Error( 'plugin_wp_php_incompatible', sprintf(
-			/* translators: %s: plugin name */
-			__( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.' ), $plugin_data['Name'] )
+		return new WP_Error(
+			'plugin_wp_php_incompatible',
+			sprintf(
+				/* translators: %s: plugin name */
+				__( '<strong>Error:</strong> Current WordPress and PHP versions do not meet minimum requirements for %s.' ),
+				$plugin_data['Name']
+			)
 		);
 	} elseif ( ! $plugin_data['php_compatible'] ) {
-		return new WP_Error( 'plugin_php_incompatible', sprintf(
-			/* translators: %s: plugin name */
-			__( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.' ), $plugin_data['Name'] )
+		return new WP_Error(
+			'plugin_php_incompatible',
+			sprintf(
+				/* translators: %s: plugin name */
+				__( '<strong>Error:</strong> Current PHP version does not meet minimum requirements for %s.' ),
+				$plugin_data['Name']
+			)
 		);
 	} elseif ( ! $plugin_data['wp_compatible'] ) {
-		return new WP_Error( 'plugin_wp_incompatible', sprintf(
-			/* translators: %s: plugin name */
-			__( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.' ), $plugin_data['Name'] )
+		return new WP_Error(
+			'plugin_wp_incompatible',
+			sprintf(
+				/* translators: %s: plugin name */
+				__( '<strong>Error:</strong> Current WordPress version does not meet minimum requirements for %s.' ),
+				$plugin_data['Name']
+			)
 		);
 	}
 
diff --git src/wp-includes/class-wp-site-query.php src/wp-includes/class-wp-site-query.php
index bc3e3519a3..ec5fa0fcda 100644
--- src/wp-includes/class-wp-site-query.php
+++ src/wp-includes/class-wp-site-query.php
@@ -87,6 +87,14 @@ class WP_Site_Query {
 	 */
 	public $sites;
 
+	/**
+	 * List of site ids in the query.
+	 *
+	 * @since 5.2.0
+	 * @var array|null
+	 */
+	public $site_ids = null;
+
 	/**
 	 * The amount of found sites for the current query.
 	 *
@@ -288,32 +296,49 @@ class WP_Site_Query {
 			$this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this );
 		}
 
-		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
-		$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
+		/**
+		 * Filter the sites array before the query takes place.
+		 *
+		 * Return a non-null value to bypass WordPress's default site queries.
+		 *
+		 *
+		 * @since 5.2.0
+		 *
+		 * @param array|null    $site_ids Return an array of site data to short-circuit WP's site query,
+		 *                                or null to allow WP to run its normal queries.
+		 * @param WP_Site_Query $this The WP_Site_Query instance, passed by reference.
+		 */
+		$this->site_ids = apply_filters_ref_array( 'sites_pre_query', array( $this->site_ids, &$this ) );
 
-		// Ignore the $fields argument as the queried result will be the same regardless.
-		unset( $_args['fields'] );
+		if ( null === $this->site_ids ) {
 
-		$key          = md5( serialize( $_args ) );
-		$last_changed = wp_cache_get_last_changed( 'sites' );
+			// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
+			$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
 
-		$cache_key   = "get_sites:$key:$last_changed";
-		$cache_value = wp_cache_get( $cache_key, 'sites' );
+			// Ignore the $fields argument as the queried result will be the same regardless.
+			unset( $_args['fields'] );
 
-		if ( false === $cache_value ) {
-			$site_ids = $this->get_site_ids();
-			if ( $site_ids ) {
-				$this->set_found_sites();
-			}
+			$key          = md5( serialize( $_args ) );
+			$last_changed = wp_cache_get_last_changed( 'sites' );
 
-			$cache_value = array(
-				'site_ids'    => $site_ids,
-				'found_sites' => $this->found_sites,
-			);
-			wp_cache_add( $cache_key, $cache_value, 'sites' );
-		} else {
-			$site_ids          = $cache_value['site_ids'];
-			$this->found_sites = $cache_value['found_sites'];
+			$cache_key   = "get_sites:$key:$last_changed";
+			$cache_value = wp_cache_get( $cache_key, 'sites' );
+
+			if ( false === $cache_value ) {
+				$this->site_ids = $this->get_site_ids();
+				if ( $this->site_ids ) {
+					$this->set_found_sites();
+				}
+
+				$cache_value = array(
+					'site_ids'    => $this->site_ids,
+					'found_sites' => $this->found_sites,
+				);
+				wp_cache_add( $cache_key, $cache_value, 'sites' );
+			} else {
+				$this->site_ids    = $cache_value['site_ids'];
+				$this->found_sites = $cache_value['found_sites'];
+			}
 		}
 
 		if ( $this->found_sites && $this->query_vars['number'] ) {
@@ -323,25 +348,25 @@ class WP_Site_Query {
 		// If querying for a count only, there's nothing more to do.
 		if ( $this->query_vars['count'] ) {
 			// $site_ids is actually a count in this case.
-			return intval( $site_ids );
+			return intval( $this->site_ids );
 		}
 
-		$site_ids = array_map( 'intval', $site_ids );
+		$this->site_ids = array_map( 'intval', $site_ids );
 
 		if ( 'ids' == $this->query_vars['fields'] ) {
-			$this->sites = $site_ids;
+			$this->sites = $this->site_ids;
 
 			return $this->sites;
 		}
 
 		// Prime site network caches.
 		if ( $this->query_vars['update_site_cache'] ) {
-			_prime_site_caches( $site_ids, $this->query_vars['update_site_meta_cache'] );
+			_prime_site_caches( $this->site_ids, $this->query_vars['update_site_meta_cache'] );
 		}
 
 		// Fetch full site objects from the primed cache.
 		$_sites = array();
-		foreach ( $site_ids as $site_id ) {
+		foreach ( $this->site_ids as $site_id ) {
 			if ( $_site = get_site( $site_id ) ) {
 				$_sites[] = $_site;
 			}
diff --git tests/phpunit/tests/multisite/siteQuery.php tests/phpunit/tests/multisite/siteQuery.php
index bac9269ff4..c17f977932 100644
--- tests/phpunit/tests/multisite/siteQuery.php
+++ tests/phpunit/tests/multisite/siteQuery.php
@@ -911,6 +911,42 @@ if ( is_multisite() ) :
 			}
 		}
 
+
+		/**
+		 * @ticket 45749
+		 */
+		public function test_sites_pre_query_filter_should_bypass_database_query() {
+			global $wpdb;
+
+			add_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
+
+			$num_queries = $wpdb->num_queries;
+
+			$q       = new WP_Site_Query();
+			$results = $q->query(
+				array(
+					'fields' => 'ids',
+				)
+			);
+
+			remove_filter( 'sites_pre_query', array( __CLASS__, 'filter_sites_pre_query' ), 10, 2 );
+
+			// Make sure no queries were executed.
+			$this->assertSame( $num_queries, $wpdb->num_queries );
+
+			// We manually inserted a non-existing site and overrode the results with it.
+			$this->assertSame( array( 555 ), $q->sites );
+
+			// Make sure manually setting total_users doesn't get overwritten.
+			$this->assertEquals( 1, $q->found_sites );
+		}
+
+		public static function filter_sites_pre_query( $sites, $query ) {
+			$query->found_sites = 1;
+
+			return array( 555 );
+		}
+
 		public function data_wp_site_query_meta_query() {
 			return array(
 				array(
