Skip to content
Discussion options

You must be logged in to vote

The last-two-digits scheme can be expressed directly with modulo. Register datasets named db00 through db99, then use a callback like this:

$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];

		return sprintf( 'db%02d', $blog_id % 100 );
	}
);

That maps blog 9 to db09, 74 to db74, 999989 to db89, and 9200 to db00. Returning null leaves network-global tables on the default dataset.

Every dataset the callback can return must already be added with $wpdb->add_database(), and every site's complete table set must…

Replies: 1 comment

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
2 participants
Converted from issue

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