Using WordPress ‘pre_get_networks’ PHP action

The pre_get_networks WordPress PHP action fires before networks are retrieved, allowing you to modify the WP_Network_Query instance.

Usage

add_action('pre_get_networks', 'my_custom_pre_get_networks');

function my_custom_pre_get_networks($query) {
  // your custom code here
  return $query;
}

Parameters

  • $query (WP_Network_Query): The current instance of WP_Network_Query, passed by reference.

More information

See WordPress Developer Resources: pre_get_networks

Examples

Modify networks query to exclude a specific network ID

This example modifies the networks query to exclude a specific network ID (e.g., 2) from the results.

add_action('pre_get_networks', 'exclude_specific_network_id');

function exclude_specific_network_id($query) {
  $query->set('exclude', array(2));
}

Order networks by their creation date

This example modifies the networks query to order the results by the network’s creation date in descending order.

add_action('pre_get_networks', 'order_networks_by_creation_date');

function order_networks_by_creation_date($query) {
  $query->set('orderby', 'registered');
  $query->set('order', 'DESC');
}

Limit the number of networks retrieved

This example limits the number of networks retrieved to 5.

add_action('pre_get_networks', 'limit_networks_count');

function limit_networks_count($query) {
  $query->set('number', 5);
}

Search for networks with a specific domain

This example retrieves networks with a domain containing the string “example”.

add_action('pre_get_networks', 'search_networks_by_domain');

function search_networks_by_domain($query) {
  $query->set('search', 'example');
  $query->set('search_columns', array('domain'));
}

Retrieve only public networks

This example modifies the networks query to retrieve only public networks.

add_action('pre_get_networks', 'get_public_networks_only');

function get_public_networks_only($query) {
  $query->set('public', 1);
}