Function and usage of get_networks() in wordpress


Answers ( 1 )


    The get_networks() function in WordPress is designed to retrieve a list of networks. This is particularly useful in a WordPress multisite installation, where you might have multiple networks, each containing several sites. Here's a detailed explanation of the function and an example of its usage:

    Function Signature

    get_networks( string|array $args = array() ): array|int

    Function Description

    • Purpose: get_networks() is used to obtain a list of networks in a WordPress multisite setup.
    • Parameters: The function accepts one parameter $args, which can be either a string or an array. This parameter specifies the criteria for the networks you want to retrieve.
    • $args Details: The $args parameter can include various arguments that filter and define the query. The specifics of these arguments can be found in the documentation for WP_Network_Query::parse_query(). These arguments allow you to specify things like:
      • The number of networks to retrieve
      • The order in which to list them
      • Search criteria for specific network attributes
    • Default Value: By default, $args is an empty array, which means it will fetch all available networks without any specific filtering.

    Return Value

    • The function returns different types of values based on the 'fields' or 'count' query vars provided in $args:
      1. List of WP_Network Objects: If 'fields' is not set or set to any value other than 'ids', it returns an array of WP_Network objects.
      2. List of Network IDs: If 'fields' is set to 'ids', it returns an array of network IDs.
      3. Number of Networks: If 'count' is passed as a query var, it returns an integer representing the number of networks.

    Sample Usage

    // Example 1: Retrieve all networks as WP_Network objects
    $networks = get_networks();
    foreach ($networks as $network) {
        echo 'Network ID: ' . $network->id . '<br>';
    // Example 2: Retrieve first 5 network IDs
    $args = array(
        'number' => 5,
        'fields' => 'ids'
    $network_ids = get_networks($args);
    foreach ($network_ids as $id) {
        echo 'Network ID: ' . $id . '<br>';
    // Example 3: Count the total number of networks
    $count_args = array('count' => true);
    $total_networks = get_networks($count_args);
    echo 'Total networks: ' . $total_networks;


    • This function is especially useful for developers working with multisite networks in WordPress.
    • Always ensure you have the appropriate permissions and capabilities to access network information when using this function.
    • The returned data can be used to perform actions or display information about each network in a multisite setup.

Leave an answer