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() {
		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 $this 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 array|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 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 (int) $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, $this->query_vars['update_site_meta_cache'] );
		}

		// 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;
	}

Changelog

Version Description
4.6.0 Introduced.

© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_site_query/get_sites