Using WordPress ‘get_network()’ PHP function

The get_network() WordPress PHP function retrieves network data given a network ID or network object.

Usage

get_network( $network );

Example:

// Retrieve network data for network ID 2
$network_data = get_network( 2 );

Parameters

  • $network (WP_Network|int|null) – Optional. Network to retrieve. Default is the current network. Default value: null.

More information

See WordPress Developer Resources: get_network()

Examples

Retrieve current network data

Retrieve the current network’s data.

$current_network = get_network();

Retrieve network data by ID

Retrieve the data for a specific network by its ID.

$network_id = 5;
$specific_network = get_network( $network_id );

Display network name

Retrieve and display the network name for a specific network ID.

$network_id = 3;
$network_data = get_network( $network_id );
echo 'Network Name: ' . $network_data->site_name;

Check if network exists

Check if a specific network exists by its ID.

$network_id = 4;
$network_data = get_network( $network_id );

if ( $network_data ) { echo 'Network exists.'; } else { echo 'Network not found.'; }

Get all network sites

Retrieve and display all sites within a specific network.

$network_id = 2;
$network_data = get_network( $network_id );
$sites = get_sites( array( 'network_id' => $network_data->id ) );

foreach ( $sites as $site ) { echo 'Site ID: ' . $site->blog_id . ', Site URL: ' . $site->siteurl . '<br>'; }