diff --git src/wp-includes/class-wp-site-query.php src/wp-includes/class-wp-site-query.php
index bc3e3519a3..5a6e015221 100644
--- src/wp-includes/class-wp-site-query.php
+++ src/wp-includes/class-wp-site-query.php
@@ -288,32 +288,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_Query   $this     The WP_Site_Query instance, passed by reference.
+		 */
+		$site_ids = apply_filters_ref_array( 'sites_pre_query', array( null, &$this ) );
 
-		// Ignore the $fields argument as the queried result will be the same regardless.
-		unset( $_args['fields'] );
+		if ( null === $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 ) {
+				$site_ids = $this->get_site_ids();
+				if ( $site_ids ) {
+					$this->set_found_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'];
+			}
 		}
 
 		if ( $this->found_sites && $this->query_vars['number'] ) {
diff --git src/wp-includes/comment.php src/wp-includes/comment.php
index 1cb9be5ada..5e89ef7695 100644
--- src/wp-includes/comment.php
+++ src/wp-includes/comment.php
@@ -756,7 +756,7 @@ function wp_allow_comment( $commentdata, $avoid_die = false ) {
 	if ( $is_flood ) {
 		/** This filter is documented in wp-includes/comment-template.php */
 		$comment_flood_message = apply_filters( 'comment_flood_message', __( 'You are posting comments too quickly. Slow down.' ) );
-		
+
 		return new WP_Error( 'comment_flood', $comment_flood_message, 429 );
 	}
 
diff --git tests/phpunit/tests/user/query.php tests/phpunit/tests/user/query.php
index 1330d8b5c9..d39b7d7530 100644
--- tests/phpunit/tests/user/query.php
+++ tests/phpunit/tests/user/query.php
@@ -1723,4 +1723,39 @@ class Tests_User_Query extends WP_UnitTestCase {
 
 		return array( 555 );
 	}
+
+	/**
+	 * @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 );
+	}
 }
