Skip to content
Discussion options

You must be logged in to vote

Yes. Route on the numeric blog ID parsed from $wpdb->table, and return the dataset for the matching range:

$wpdb->add_callback(
	function ( $query, $wpdb ) {
		$pattern = '/^' . preg_quote( $wpdb->base_prefix, '/' ) . '(\d+)_/';

		if ( ! preg_match( $pattern, $wpdb->table, $matches ) ) {
			return null;
		}

		$blog_id = (int) $matches[1];

		if ( $blog_id >= 2 && $blog_id <= 5 ) {
			return 'child_db_2_5';
		}

		if ( $blog_id <= 10 ) {
			return 'child_db_6_10';
		}

		if ( $blog_id <= 15 ) {
			return 'child_db_11_15';
		}

		return null;
	}
);

Add each returned dataset with $wpdb->add_database() first. The site's tables must already exist in that physical database, and related tables…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by JJJ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #71 on June 12, 2024 21:58.