Using WordPress ‘networks_clauses’ PHP filter

The networks_clauses filter allows you to modify the network query clauses used when querying for networks in a WordPress multisite installation.

Usage

add_filter('networks_clauses', 'my_networks_clauses_filter', 10, 2);
function my_networks_clauses_filter($clauses, $query) {
    // your custom code here
    return $clauses;
}

Parameters

  • $clauses (string[]): An associative array of network query clauses.
  • $query (WP_Network_Query): The current instance of WP_Network_Query (passed by reference).

Examples

Modify the ORDER BY clause

add_filter('networks_clauses', 'modify_networks_order_by', 10, 2);
function modify_networks_order_by($clauses, $query) {
    $clauses['orderby'] = 'ORDER BY domain ASC';
    return $clauses;
}

This example modifies the ORDER BY clause to sort networks by their domain in ascending order.

Add a custom WHERE clause

add_filter('networks_clauses', 'add_custom_where_clause', 10, 2);
function add_custom_where_clause($clauses, $query) {
    $clauses['where'] .= ' AND domain LIKE "%example%"';
    return $clauses;
}

This example adds a custom WHERE clause to the network query, filtering networks with ‘example’ in their domain name.

Modify LIMIT and OFFSET

add_filter('networks_clauses', 'modify_limit_and_offset', 10, 2);
function modify_limit_and_offset($clauses, $query) {
    $clauses['limits'] = 'LIMIT 10 OFFSET 20';
    return $clauses;
}

This example modifies the LIMIT and OFFSET values in the network query to display 10 networks starting from the 21st network.

Modify the JOIN clause

add_filter('networks_clauses', 'modify_networks_join', 10, 2);
function modify_networks_join($clauses, $query) {
    global $wpdb;
    $clauses['join'] .= " LEFT JOIN {$wpdb->site_meta} ON ({$wpdb->site}.id = {$wpdb->site_meta}.site_id)";
    return $clauses;
}

This example modifies the JOIN clause to include the site_meta table in the network query.

Add a GROUP BY clause

add_filter('networks_clauses', 'add_group_by_clause', 10, 2);
function add_group_by_clause($clauses, $query) {
    $clauses['groupby'] = 'GROUP BY domain';
    return $clauses;
}

This example adds a GROUP BY clause to the network query, grouping the networks by their domain.