How to Change the Primary Blog (Site) in WordPress Multisite

Below we will consider how to change the primary site (blog) of the network in WordPress Multisite. For example, we already have a network of sites installed and running, and now we need to change the primary site of the network — make the primary not the main, but make one of the sub-sites the primary.

To figure out how WP determines the main site yourself, I would advise digging into the core and examining the method WP_Network::get_main_site_id(). In principle, there you will find all answers on how the main site (blog) of the network is determined and, based on this information, understand how and what needs to be done so as not to spoil anything.

To perform the transformation you need to make changes in wp-config.php, as well as in the database.

Step 1: Create a backup of files and database

Before you start changing the data, you must обязательно create a backup. Both for the database and for the files.

Step 2: Edit wp-config.php

Open the file and find the lines:

define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 1 );

The number 1 indicates which of the sites is the network's site and which of the sites is the primary blog in the current network. Usually this is the same site (blog).

To find out the IDs you should enter here, you need to open your database — the table wp_blogs:

WordPress table wp_blogs.

There you will find columns blog_id and site_id, as well as the domains and paths associated with them.

Now specify the required blog_id in wp_config.php and save the file. In our case it will be (see below):

define( 'SITE_ID_CURRENT_SITE', 1 );
define( 'BLOG_ID_CURRENT_SITE', 2 );

SITE_ID_CURRENT_SITE is usually not changed in 99% of cases you have only one network of sites; the network ID is specified in the table wp_sites.

The value of blog_id for BLOG_ID_CURRENT_SITE is the blog you want to make the primary site, it can also be found on the admin page: My Sites > Network Admin > My Sites.

Step 3: Database configuration

If you skipped Step 2, you need to open the WordPress database and go to the table wp_blogs.

To set the new site (blog) as the primary, you need to configure the domain column. Here you need to enter the value from the original page.

Before changing the domain field in the wp_blogs table.
After changing the domain field in the wp_blogs table.

This completes the domain configuration.

Step 4: Editing the wp_site table

Now switch to the wp_site table. There you will find a record with fields id, domain and path.

Here you should ensure that the value in the domain column is the same as in the wp_blogs table. That is the entry you changed.

Change the domain field in the WordPress wp_site table.

Step 5: Adapt the wp_post table.

If you changed the domain or path of the site, you need to fix all links in the database related to these changes. For example, links to images.

The best way is to do this via the WP_CLI command search-replace. Or you can use a direct query:

UPDATE wp_posts SET post_content = REPLACE ( post_content, 'http://old-url', 'http://new-url' );

IMPORTANT! A direct query can corrupt data in the database if you replace in other tables and fields, for example in wp_postmeta. The thing is that the URL (which is replaced) may reside in a serialized array, and if so, such a replacement will break serialized data.