Retrieves a list of sites matching the query vars.
Source
public function get_sites() {
global $wpdb;
$this->parse_query();
// Parse meta query.
$this->meta_query = new WP_Meta_Query();
$this->meta_query->parse_query_vars( $this->query_vars );
/**
* Fires before sites are retrieved.
*
* @since 4.6.0
*
* @param WP_Site_Query $query Current instance of WP_Site_Query (passed by reference).
*/
do_action_ref_array( 'pre_get_sites', array( &$this ) );
// Reparse query vars, in case they were modified in a 'pre_get_sites' callback.
$this->meta_query->parse_query_vars( $this->query_vars );
if ( ! empty( $this->meta_query->queries ) ) {
$this->meta_query_clauses = $this->meta_query->get_sql( 'blog', $wpdb->blogs, 'blog_id', $this );
}
$site_data = null;
/**
* Filters the site data before the get_sites query takes place.
*
* Return a non-null value to bypass WordPress' default site queries.
*
* The expected return type from this filter depends on the value passed
* in the request query vars:
* - When `$this->query_vars['count']` is set, the filter should return
* the site count as an integer.
* - When `'ids' === $this->query_vars['fields']`, the filter should return
* an array of site IDs.
* - Otherwise the filter should return an array of WP_Site objects.
*
* Note that if the filter returns an array of site data, it will be assigned
* to the `sites` property of the current WP_Site_Query instance.
*
* Filtering functions that require pagination information are encouraged to set
* the `found_sites` and `max_num_pages` properties of the WP_Site_Query object,
* passed to the filter by reference. If WP_Site_Query does not perform a database
* query, it will not have enough information to generate these values itself.
*
* @since 5.2.0
* @since 5.6.0 The returned array of site data is assigned to the `sites` property
* of the current WP_Site_Query instance.
*
* @param WP_Site[]|int[]|int|null $site_data Return an array of site data to short-circuit WP's site query,
* the site count as an integer if `$this->query_vars['count']` is set,
* or null to run the normal queries.
* @param WP_Site_Query $query The WP_Site_Query instance, passed by reference.
*/
$site_data = apply_filters_ref_array( 'sites_pre_query', array( $site_data, &$this ) );
if ( null !== $site_data ) {
if ( is_array( $site_data ) && ! $this->query_vars['count'] ) {
$this->sites = $site_data;
}
return $site_data;
}
// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
$_args = wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) );
// Ignore the $fields, $update_site_cache, $update_site_meta_cache argument as the queried result will be the same regardless.
unset( $_args['fields'], $_args['update_site_cache'], $_args['update_site_meta_cache'] );
$key = md5( serialize( $_args ) );
$last_changed = wp_cache_get_last_changed( 'sites' );
$cache_key = "get_sites:$key:$last_changed";
$cache_value = wp_cache_get( $cache_key, 'site-queries' );
if ( false === $cache_value ) {
$site_ids = $this->get_site_ids();
if ( $site_ids ) {
$this->set_found_sites();
}
$cache_value = array(
'site_ids' => $site_ids,
'found_sites' => $this->found_sites,
);
wp_cache_add( $cache_key, $cache_value, 'site-queries' );
} else {
$site_ids = $cache_value['site_ids'];
$this->found_sites = $cache_value['found_sites'];
}
if ( $this->found_sites && $this->query_vars['number'] ) {
$this->max_num_pages = (int) ceil( $this->found_sites / $this->query_vars['number'] );
}
// If querying for a count only, there's nothing more to do.
if ( $this->query_vars['count'] ) {
// $site_ids is actually a count in this case.
return (int) $site_ids;
}
$site_ids = array_map( 'intval', $site_ids );
if ( $this->query_vars['update_site_meta_cache'] ) {
wp_lazyload_site_meta( $site_ids );
}
if ( 'ids' === $this->query_vars['fields'] ) {
$this->sites = $site_ids;
return $this->sites;
}
// Prime site network caches.
if ( $this->query_vars['update_site_cache'] ) {
_prime_site_caches( $site_ids, false );
}
// Fetch full site objects from the primed cache.
$_sites = array();
foreach ( $site_ids as $site_id ) {
$_site = get_site( $site_id );
if ( $_site ) {
$_sites[] = $_site;
}
}
/**
* Filters the site query results.
*
* @since 4.6.0
*
* @param WP_Site[] $_sites An array of WP_Site objects.
* @param WP_Site_Query $query Current instance of WP_Site_Query (passed by reference).
*/
$_sites = apply_filters_ref_array( 'the_sites', array( $_sites, &$this ) );
// Convert to WP_Site instances.
$this->sites = array_map( 'get_site', $_sites );
return $this->sites;
}
Hooks
- do_action_ref_array( ‘pre_get_sites’,
WP_Site_Query $query ) Fires before sites are retrieved.
- apply_filters_ref_array( ‘sites_pre_query’,
WP_Site[]|int[]|int|null $site_data ,WP_Site_Query $query ) Filters the site data before the get_sites query takes place.
- apply_filters_ref_array( ‘the_sites’,
WP_Site[] $_sites ,WP_Site_Query $query ) Filters the site query results.
Changelog
Version | Description |
---|---|
4.6.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.