Using WordPress ‘networks_pre_query’ PHP filter

networks_pre_query is a WordPress PHP filter that allows you to modify the network data before the query is executed. You can return a non-null value to bypass the default WordPress network queries.

Usage

add_filter('networks_pre_query', 'your_custom_function', 10, 2);
function your_custom_function($network_data, $query) {
    // your custom code here
    return $network_data;
}

Parameters

  • $network_data (array|int|null) – The data to return, either an array of network data, an integer count of networks, or null to let WordPress perform its normal queries.
  • $query (WP_Network_Query) – The WP_Network_Query instance, passed by reference.

Examples

Return a specific network by ID

add_filter('networks_pre_query', 'get_specific_network', 10, 2);

function get_specific_network($network_data, $query) {
    if (1 === $query->query_vars['network_id']) {
        // Return a specific network with ID 1
        $network_data = array(new WP_Network(1));
    }

    return $network_data;
}

This code checks if the requested network ID is 1 and returns the corresponding WP_Network object.

Return a count of networks

add_filter('networks_pre_query', 'get_network_count', 10, 2);

function get_network_count($network_data, $query) {
    if (isset($query->query_vars['count'])) {
        // Return a count of networks
        $network_data = 5;
    }

    return $network_data;
}

This code returns the count of networks as an integer when the ‘count’ query variable is set.

Return an array of network IDs

add_filter('networks_pre_query', 'get_network_ids', 10, 2);

function get_network_ids($network_data, $query) {
    if ('ids' === $query->query_vars['fields']) {
        // Return an array of network IDs
        $network_data = array(1, 2, 3, 4, 5);
    }

    return $network_data;
}

This code returns an array of network IDs when the ‘fields’ query variable is set to ‘ids’.

Set pagination properties

add_filter('networks_pre_query', 'set_pagination_properties', 10, 2);

function set_pagination_properties($network_data, $query) {
    // Your custom code to get network data
    $network_data = array(new WP_Network(1), new WP_Network(2));

    // Set pagination properties
    $query->found_networks = 2;
    $query->max_num_pages = 1;

    return $network_data;
}

This code sets the found_networks and max_num_pages properties of the WP_Network_Query object after retrieving the network data.

Return network data based on a custom condition

add_filter('networks_pre_query', 'get_networks_by_custom_condition', 10, 2);

function get_networks_by_custom_condition($network_data, $query) {
    if ('custom_condition' === $query->query_vars['your_custom_var']) {
        // Your custom code to get network data based on the custom condition
        $network_data = array(new WP_Network(1), new WP_Network(2));
    }

    return $network_data;
}

This code returns network data based on a custom condition.

Filter networks based on custom meta value

add_filter('networks_pre_query', 'filter_networks_by_custom_meta', 10, 2);

function filter_networks_by_custom_meta($network_data, $query) {
    if (isset($query->query_vars['custom_meta_key']) && isset($query->query_vars['custom_meta_value'])) {
        // Your custom code to get network data based on the custom meta value
        $network_data = array(new WP_Network(3), new WP_Network(4));
    }

    return $network_data;
}

This code filters the networks based on a custom meta key and value pair specified in the query variables.

Exclude specific networks from the result

add_filter('networks_pre_query', 'exclude_specific_networks', 10, 2);

function exclude_specific_networks($network_data, $query) {
    // Get all network data
    $all_networks = array(new WP_Network(1), new WP_Network(2), new WP_Network(3), new WP_Network(4));

    // Filter out specific networks (e.g., with IDs 2 and 4)
    $excluded_network_ids = array(2, 4);
    $network_data = array_filter($all_networks, function ($network) use ($excluded_network_ids) {
        return !in_array($network->id, $excluded_network_ids);
    });

    return $network_data;
}

This code retrieves all networks and filters out specific networks (e.g., with IDs 2 and 4) before returning the data.

Change the network order based on a custom condition

add_filter('networks_pre_query', 'change_network_order', 10, 2);

function change_network_order($network_data, $query) {
    if ('custom_order' === $query->query_vars['your_custom_order_var']) {
        // Get all network data and reorder based on your custom condition
        $network_data = array(new WP_Network(4), new WP_Network(3), new WP_Network(2), new WP_Network(1));
    }

    return $network_data;
}

This code reorders the networks based on a custom condition specified in the query variables.

Return networks only with a specific domain

add_filter('networks_pre_query', 'get_networks_by_domain', 10, 2);

function get_networks_by_domain($network_data, $query) {
    if (isset($query->query_vars['domain'])) {
        // Your custom code to get network data based on the specified domain
        $network_data = array(new WP_Network(1), new WP_Network(2));
    }

    return $network_data;
}

This code returns networks that have the specified domain in the query variables.

10. Return networks with a specific path

add_filter('networks_pre_query', 'get_networks_by_path', 10, 2);

function get_networks_by_path($network_data, $query) {
    if (isset($query->query_vars['path'])) {
        // Your custom code to get network data based on the specified path
        $network_data = array(new WP_Network(3), new WP_Network(4));
    }

    return $network_data;
}

This code returns networks that have the specified path in the query variables.