Using WordPress ‘parse_site_query’ PHP action

The parse_site_query WordPress PHP action fires after the site query vars have been parsed.

Usage

add_action('parse_site_query', 'your_custom_function');
function your_custom_function($query) {
  // your custom code here
}

Parameters

  • $query (WP_Site_Query): The WP_Site_Query instance (passed by reference).

More information

See WordPress Developer Resources: parse_site_query

Examples

Modify the query before it’s executed

Modify the site query before it’s executed to display only public sites.

add_action('parse_site_query', 'modify_site_query');
function modify_site_query($query) {
  $query->set('public', 1);
}

Log site query information

Log the site query information for debugging purposes.

add_action('parse_site_query', 'log_site_query');
function log_site_query($query) {
  error_log(print_r($query->query_vars, true));
}

Customize the site query for specific pages

Customize the site query to display only public sites on the homepage and private sites on a specific page.

add_action('parse_site_query', 'customize_site_query');
function customize_site_query($query) {
  if (is_front_page()) {
    $query->set('public', 1);
  } elseif (is_page('private-sites')) {
    $query->set('public', 0);
  }
}

Modify the site query to exclude a specific site

Exclude a specific site from the site query results.

add_action('parse_site_query', 'exclude_specific_site');
function exclude_specific_site($query) {
  $excluded_sites = array(5); // Site IDs to exclude
  $query->set('exclude', $excluded_sites);
}

Sort site query results by site creation date

Sort the site query results by the site creation date in descending order.

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