WordPress at a glance

get_sites() WP 4.6.0

Retrieves a list of sites in the form of array in the current network matching requested arguments.

Until version 4.6, wp_get_sites() was used instead of this function.

If the network has more than 10 000 websites then an empty array will be returned. This check is done by wp_is_large_network() function, the result of which can be filtered via wp_is_large_network hook.

Is the basis for: get_id_from_blogname()
Works based on: WP_Site_Query()

No Hooks.

Return

Array/Int.

  • List of WP_Site objects.
  • List of site ids when fields = 'ids'.
  • The number of sites when 'count' is passed as a query var.

Example:

Array(
	[0] => WP_Site Object(
		[blog_id] => 1
		[site_id] => 1
		[domain] => example.com
		[path] => /
		[registered] => 2013-11-08 17:56:46
		[last_updated] => 2013-11-08 18:57:19
		[public] => 1
		[archived] => 0
		[mature] => 0
		[spam] => 0
		[deleted] => 0
		[lang_id] => 0
	)

	[1] => WP_Site Object(
		[blog_id] => 2
		...
	)
)

Usage Template

$sites = get_sites( array(
	'fields'            => '',
	'ID'                => '',
	'site__in'          => '',
	'site__not_in'      => '',
	'number'            => 100,
	'offset'            => '',
	'no_found_rows'     => true,
	'orderby'           => 'id',
	'order'             => 'ASC',
	'network_id'        => 0,
	'network__in'       => array(),
	'network__not_in'   => array(),
	'domain'            => '',
	'domain__in'        => array(),
	'domain__not_in'    => array(),
	'path'              => '',
	'path__in'          => array(),
	'path__not_in'      => array(),
	'lang_id'           => 0,
	'lang__in'          => array(),
	'lang__not_in'      => array(),
	'public'            => null,
	'archived'          => null,
	'mature'            => null,
	'spam'              => null,
	'deleted'           => null,
	'search'            => '',
	'search_columns'    => array(),
	'count'             => false,
	'date_query'        => null, // See WP_Date_Query
	'update_site_cache' => true,
) );

foreach( $sites as $site ){
	// делаем что либо
}

Usage

get_sites( $args );
$args(string/array)

Array or query string of site query parameters.

  • $site__in (array)
    Array of site IDs to include.
    Default: ''

  • $site__not_in (array)
    Array of site IDs to exclude.
    Default: ''

  • $count (true/false)
    Whether to return a site count (true) or array of site objects.
    Default: false

  • $date_query (array)
    Date query clauses to limit sites by. See WP_Date_Query.
    Default: null

  • $fields (string)
    Site fields to return. Accepts 'ids' (returns an array of site IDs) or empty (returns an array of complete site objects).
    Default: ''

  • $ID (int)
    A site ID to only return that site.
    Default: ''

  • $number (int)
    Maximum number of sites to retrieve.
    Default: 100

  • $offset (int)
    Number of sites to offset the query. Used to build LIMIT clause.

  • $no_found_rows (true/false)
    Whether to disable the SQL_CALC_FOUND_ROWS query.
    Default: true

  • $orderby (string/array)
    Site status or array of statuses. Accepts 'id', 'domain', 'path', 'network_id', 'last_updated', 'registered', 'domain_length', 'path_length', 'site__in' and 'network__in'. Also accepts false, an empty array, or 'none' to disable ORDER BY clause.
    Default: 'id'

  • $order (string)
    How to order retrieved sites. Accepts 'ASC', 'DESC'.
    Default: 'ASC'

  • $network_id (int)
    Limit results to those affiliated with a given network ID. If 0, include all networks.

  • $network__in (array)
    Array of network IDs to include affiliated sites for.
    Default: ''

  • $network__not_in (array)
    Array of network IDs to exclude affiliated sites for.
    Default: ''

  • $domain (string)
    Limit results to those affiliated with a given domain.
    Default: ''

  • $domain__in (array)
    Array of domains to include affiliated sites for.
    Default: ''

  • $domain__not_in (array)
    Array of domains to exclude affiliated sites for.
    Default: ''

  • $path (string)
    Limit results to those affiliated with a given path.
    Default: ''

  • $path__in (array)
    Array of paths to include affiliated sites for.
    Default: ''

  • $path__not_in (array)
    Array of paths to exclude affiliated sites for.
    Default: ''

  • $public (int)
    Limit results to public sites. Accepts '1' or '0'.
    Default: ''

  • $archived (int)
    Limit results to archived sites. Accepts '1' or '0'.
    Default: ''

  • $mature (int)
    Limit results to mature sites. Accepts '1' or '0'.
    Default: ''

  • $spam (int)
    Limit results to spam sites. Accepts '1' or '0'.
    Default: ''

  • $deleted (int)
    Limit results to deleted sites. Accepts '1' or '0'.
    Default: ''

  • $lang_id (int)
    Limit results to a language ID.
    Default: ''

  • $lang__in (array)
    Array of language IDs to include affiliated sites for.
    Default: ''

  • $lang__not_in (array)
    Array of language IDs to exclude affiliated sites for.
    Default: ''

  • $search (string)
    Search term(s) to retrieve matching sites for.
    Default: ''

  • $search_columns (array)
    Array of column names to be searched. Accepts 'domain' and 'path'.
    Default: empty array

  • $update_site_cache (true/false)
    Whether to prime the cache for found sites.
    Default: true

Default: ''

Examples

#1 Display the domains of all sites in the current network

$sites = get_sites();

foreach( $sites as $site ){

	echo $site->domain . $site->path;
	echo ' - '. get_blog_details( $site->blog_id )->blogname;
	echo '<br>';

}

Notes

  • See: WP_Site_Query::parse_query()

Changelog

Since 4.6.0 Introduced.
Since 4.8.0 Introduced the 'lang_id', 'lang__in', and 'lang__not_in' parameters.

Code of get sites: wp-includes/ms-site.php WP 5.2.2

<?php
function get_sites( $args = array() ) {
	$query = new WP_Site_Query();

	return $query->query( $args );
}

Related Functions

From category: Multisite

No comments
    Hello, !     Log In . Register