Using WordPress ‘found_sites_query’ PHP filter

The found_sites_query WordPress PHP filter allows you to modify the query used to retrieve the found site count.

Usage

add_filter('found_sites_query', 'my_custom_found_sites_query', 10, 2);
function my_custom_found_sites_query($found_sites_query, $site_query) {
    // your custom code here
    return $found_sites_query;
}

Parameters

  • $found_sites_query (string) – The SQL query used to retrieve found site count. Default is ‘SELECT FOUND_ROWS()’.
  • $site_query (WP_Site_Query) – The WP_Site_Query instance.

More information

See WordPress Developer Resources: found_sites_query

Examples

Change the found_sites_query

Modify the default found_sites_query to use a custom SQL query.

add_filter('found_sites_query', 'change_found_sites_query', 10, 2);
function change_found_sites_query($found_sites_query, $site_query) {
    $found_sites_query = 'SELECT COUNT(*) FROM wp_blogs';
    return $found_sites_query;
}

Exclude specific sites from count

Exclude specific sites from the found_sites_query by their site ID.

add_filter('found_sites_query', 'exclude_sites_from_count', 10, 2);
function exclude_sites_from_count($found_sites_query, $site_query) {
    $found_sites_query = str_replace('SELECT FOUND_ROWS()', 'SELECT COUNT(*) FROM wp_blogs WHERE blog_id NOT IN (2, 3)', $found_sites_query);
    return $found_sites_query;
}

Filter sites by domain

Filter the found_sites_query to count only sites with a specific domain.

add_filter('found_sites_query', 'filter_sites_by_domain', 10, 2);
function filter_sites_by_domain($found_sites_query, $site_query) {
    $found_sites_query = 'SELECT COUNT(*) FROM wp_blogs WHERE domain LIKE "%example.com%"';
    return $found_sites_query;
}

Filter sites by creation date

Filter the found_sites_query to count only sites created within a specific date range.

add_filter('found_sites_query', 'filter_sites_by_creation_date', 10, 2);
function filter_sites_by_creation_date($found_sites_query, $site_query) {
    $start_date = '2023-01-01';
    $end_date = '2023-03-31';
    $found_sites_query = "SELECT COUNT(*) FROM wp_blogs WHERE registered >= '$start_date' AND registered <= '$end_date'";
    return $found_sites_query;
}

Display the total number of sites

Display the total number of sites using the found_sites_query filter.

add_filter('found_sites_query', 'display_total_sites_count', 10, 2);
function display_total_sites_count($found_sites_query, $site_query) {
    global $wpdb;
    $total_sites = $wpdb->get_var($found_sites_query);
    echo 'Total Sites: ' . $total_sites;
    return $found_sites_query;
}