WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can one get the blog ID as well as the database prefix of the main blog in a wordpress multisite installation? Is the main blog's ID always 1? Also how can the main blog's table prefix be acquired from within the mu-plugin's code?

share|improve this question
up vote 5 down vote accepted

Use the constant BLOG_ID_CURRENT_SITE. And to get the database table prefix, use;

$main_blog_prefix = $wpdb->get_blog_prefix(BLOG_ID_CURRENT_SITE);
share|improve this answer
Thanks for the answer. Mind if I ask when BLOG_ID_CURRENT_SITE!=1? – MikeSchinkel Dec 25 '10 at 0:22
@MikeSchinkel I'm not sure! It might be if you have multiple networks, or if you (somehow or other) switched the main blog with another? – TheDeadMedic Dec 25 '10 at 1:59
Thanks. Andrew Nacin answered me on Twitter. twitter.com/nacin/status/18479360935723009 – MikeSchinkel Dec 25 '10 at 4:41
Defense against link rot: One more Q: Why would I want to set BLOG_ID_CURRENT_SITE to a number other than 1 in /wp-config.php. Or would I ever? –You might, but only if you want to change the main site. Typically, it'll be wp_site.site_id = 1 and wp_blogs.blog_id = 1. – Geoffrey Booth Sep 7 '14 at 15:44

This is how we do it to be 100% as we have multiple main sites in the network installation:

function name_ly_get_main_blog_id () {
  global $current_site;
  global $wpdb;

  return $wpdb->get_var ( $wpdb->prepare ( "SELECT `blog_id` FROM `$wpdb->blogs` WHERE `domain` = '%s' AND `path` = '%s' ORDER BY `blog_id` ASC LIMIT 1", $current_site->domain, $current_site->path ) );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.