Using WordPress ‘parse_network_query’ PHP action

The parse_network_query WordPress PHP action fires after the network query vars have been parsed.

Usage

add_action('parse_network_query', 'my_custom_parse_network_query');
function my_custom_parse_network_query($query) {
    // Your custom code here
}

Parameters

  • $query (WP_Network_Query) – The WP_Network_Query instance (passed by reference).

More information

See WordPress Developer Resources: parse_network_query

Examples

Modify the network query after parsing

Modify the network query after it has been parsed to filter out networks based on a custom criteria.

add_action('parse_network_query', 'my_custom_parse_network_query');
function my_custom_parse_network_query($query) {
    // Filter out networks with a certain ID
    $query->set('exclude', array(5, 9, 15));
}

Add a custom order to the network query

Change the order of the networks in the query results.

add_action('parse_network_query', 'my_custom_parse_network_query_order');
function my_custom_parse_network_query_order($query) {
    // Order networks by path in ascending order
    $query->set('orderby', 'path');
    $query->set('order', 'ASC');
}

Limit the number of networks returned

Limit the number of networks returned by the query.

add_action('parse_network_query', 'my_custom_parse_network_query_limit');
function my_custom_parse_network_query_limit($query) {
    // Set the maximum number of networks to return
    $query->set('number', 5);
}

Add a custom search parameter

Search networks based on a custom search parameter.

add_action('parse_network_query', 'my_custom_parse_network_query_search');
function my_custom_parse_network_query_search($query) {
    // Search for networks with 'example' in their domain
    $query->set('search', '*.example.*');
}

Combine multiple parameters for a custom network query

Modify the network query using multiple parameters for more specific results.

add_action('parse_network_query', 'my_custom_parse_network_query_combined');
function my_custom_parse_network_query_combined($query) {
    // Set custom query parameters
    $query->set('number', 5);
    $query->set('orderby', 'domain');
    $query->set('order', 'DESC');
    $query->set('search', '*.example.*');
}