WP_Site_Query::get_sites()
Retrieves a list of sites matching the query vars.
Return
(array|int) List of WP_Site objects, a list of site IDs when 'fields' is set to 'ids', or the number of sites when 'count' is passed as a query var.
Source
File: wp-includes/class-wp-site-query.php
public function get_sites() {
$this->parse_query();
/**
* Fires before sites are retrieved.
*
* @since WP-4.6.0
*
* @param WP_Site_Query $this Current instance of WP_Site_Query (passed by reference).
*/
do_action_ref_array( 'pre_get_sites', array( &$this ) );
// $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 argument as the queried result will be the same regardless.
unset( $_args['fields'] );
$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, 'sites' );
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, 'sites' );
} 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 = 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 intval( $site_ids );
}
$site_ids = array_map( 'intval', $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 );
}
// Fetch full site objects from the primed cache.
$_sites = array();
foreach ( $site_ids as $site_id ) {
if ( $_site = get_site( $site_id ) ) {
$_sites[] = $_site;
}
}
/**
* Filters the site query results.
*
* @since WP-4.6.0
*
* @param array $_sites An array of WP_Site objects.
* @param WP_Site_Query $this 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;
}
Changelog
Version | Description |
---|---|
4.6.0 | Introduced. |