get_main_network_id

The timeline below displays how wordpress function get_main_network_id has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Gets the main network ID.
 *
 * @since 4.3.0
 *
 * @return int The ID of the main network.
 */
function get_main_network_id()
{
    if (!is_multisite()) {
        return 1;
    }
    $current_network = get_network();
    if (defined('PRIMARY_NETWORK_ID')) {
        $main_network_id = PRIMARY_NETWORK_ID;
    } elseif (isset($current_network->id) && 1 === (int) $current_network->id) {
        // If the current network has an ID of 1, assume it is the main network.
        $main_network_id = 1;
    } else {
        $_networks = get_networks(array('fields' => 'ids', 'number' => 1));
        $main_network_id = array_shift($_networks);
    }
    /**
     * Filters the main network ID.
     *
     * @since 4.3.0
     *
     * @param int $main_network_id The ID of the main network.
     */
    return (int) apply_filters('get_main_network_id', $main_network_id);
}

WordPress Version: 4.7

/**
 * Get the main network ID.
 *
 * @since 4.3.0
 *
 * @return int The ID of the main network.
 */
function get_main_network_id()
{
    if (!is_multisite()) {
        return 1;
    }
    $current_network = get_network();
    if (defined('PRIMARY_NETWORK_ID')) {
        $main_network_id = PRIMARY_NETWORK_ID;
    } elseif (isset($current_network->id) && 1 === (int) $current_network->id) {
        // If the current network has an ID of 1, assume it is the main network.
        $main_network_id = 1;
    } else {
        $_networks = get_networks(array('fields' => 'ids', 'number' => 1));
        $main_network_id = array_shift($_networks);
    }
    /**
     * Filters the main network ID.
     *
     * @since 4.3.0
     *
     * @param int $main_network_id The ID of the main network.
     */
    return (int) apply_filters('get_main_network_id', $main_network_id);
}

WordPress Version: 4.6

/**
 * Get the main network ID.
 *
 * @since 4.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @return int The ID of the main network.
 */
function get_main_network_id()
{
    global $wpdb;
    if (!is_multisite()) {
        return 1;
    }
    $current_site = get_current_site();
    if (defined('PRIMARY_NETWORK_ID')) {
        $main_network_id = PRIMARY_NETWORK_ID;
    } elseif (isset($current_site->id) && 1 === (int) $current_site->id) {
        // If the current network has an ID of 1, assume it is the main network.
        $main_network_id = 1;
    } else {
        $main_network_id = wp_cache_get('primary_network_id', 'site-options');
        if (false === $main_network_id) {
            $main_network_id = (int) $wpdb->get_var("SELECT id FROM {$wpdb->site} ORDER BY id LIMIT 1");
            wp_cache_add('primary_network_id', $main_network_id, 'site-options');
        }
    }
    /**
     * Filters the main network ID.
     *
     * @since 4.3.0
     *
     * @param int $main_network_id The ID of the main network.
     */
    return (int) apply_filters('get_main_network_id', $main_network_id);
}

WordPress Version: 4.3

/**
 * Get the main network ID.
 *
 * @since 4.3.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @return int The ID of the main network.
 */
function get_main_network_id()
{
    global $wpdb;
    if (!is_multisite()) {
        return 1;
    }
    if (defined('PRIMARY_NETWORK_ID')) {
        $main_network_id = PRIMARY_NETWORK_ID;
    } elseif (1 === (int) get_current_site()->id) {
        // If the current network has an ID of 1, assume it is the main network.
        $main_network_id = 1;
    } else {
        $main_network_id = wp_cache_get('primary_network_id', 'site-options');
        if (false === $main_network_id) {
            $main_network_id = (int) $wpdb->get_var("SELECT id FROM {$wpdb->site} ORDER BY id LIMIT 1");
            wp_cache_add('primary_network_id', $main_network_id, 'site-options');
        }
    }
    /**
     * Filter the main network ID.
     *
     * @since 4.3.0
     *
     * @param int $main_network_id The ID of the main network.
     */
    return (int) apply_filters('get_main_network_id', $main_network_id);
}