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?

4 Answers 4


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);
  • Thanks for the answer. Mind if I ask when BLOG_ID_CURRENT_SITE!=1? Commented Dec 25, 2010 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? Commented Dec 25, 2010 at 1:59
  • Thanks. Andrew Nacin answered me on Twitter. twitter.com/nacin/status/18479360935723009 Commented Dec 25, 2010 at 4:41
  • 2
    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. Commented Sep 7, 2014 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 ) );

You can use the below to get the table prefix.

current site table prefix : $wpdb->prefix;
Main blog table prefix : $wpdb->base_prefix;

There's a function to get main site id for a given network (defaults to the active network)


If you only need db prefix, $wpdb->get_blog_prefix(get_main_site_id()) would suffice, but otherwise you can switch whole code blocks into other blog's context seamlessly with switch_to_blog() and restore_current_blog() functions

global $wpdb; // it's now in the main site's context
$prefix = $wpdb->prefix;
// ... do your stuff

But be wary of recursive calls to switch_to_blog(), because then context becomes hard to track and debug

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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