Using WordPress ‘get_blog_count()’ PHP function

The get_blog_count() WordPress PHP function retrieves the number of active sites on the installation.

Usage

$active_sites = get_blog_count($network_id);

Example:

Input:

$active_sites = get_blog_count();
echo "There are " . $active_sites . " active sites.";

Output:

There are 10 active sites.

Parameters

  • $network_id int|null Optional ID of the network. Default is the current network. Default: null

More information

See WordPress Developer Resources: get_blog_count()

Examples

Display active sites count

This example displays the number of active sites on the current network.

$active_sites = get_blog_count();
echo "There are " . $active_sites . " active sites.";

Display active sites count on a specific network

This example displays the number of active sites on a specific network with a given $network_id.

$network_id = 2;
$active_sites = get_blog_count($network_id);
echo "There are " . $active_sites . " active sites on network ID " . $network_id . ".";

Display a message based on the active sites count

This example displays a message based on the number of active sites.

$active_sites = get_blog_count();

if ($active_sites < 10) {
    echo "We have less than 10 active sites.";
} else {
    echo "We have 10 or more active sites.";
}

Compare active sites count between two networks

This example compares the number of active sites between two networks with $network_id1 and $network_id2.

$network_id1 = 1;
$network_id2 = 2;
$active_sites1 = get_blog_count($network_id1);
$active_sites2 = get_blog_count($network_id2);

echo "Network ID " . $network_id1 . " has " . $active_sites1 . " active sites.\n";
echo "Network ID " . $network_id2 . " has " . $active_sites2 . " active sites.";

Update a network’s active sites count

This example updates the number of active sites on a given network with $network_id and displays the new count.

$network_id = 2;
update_blog_count($network_id);
$active_sites = get_blog_count($network_id);

echo "The updated count of active sites on network ID " . $network_id . " is " . $active_sites . ".";