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.

1 time — 0.000390 sec (fast) | 50000 times — 1.52764 sec (fast)

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 string of arguments. See WP_Site_Query::__construct() for information on accepted arguments.
Default: empty array

$args Arguments

site__in(array)
An array of site IDs to get.
Default: ''
site__not_in(array)
An array of site IDs to exclude.
Default: ''
count(logical)
Put true when you want to get the number of sites found and not the data objects of those sites.
Default: false
date_query(array)
Query filtering by date. See: WP_Date_Query.
Default: null
fields(string)
Fields to be returned in the summary data. Can be: 'ids' - will return only site IDs, '' - will return all site data.
Default: ''
ID(number)
site ID, when you want to get only this site.
Default: ''
number(number)
Limit - the maximum number of sites to be retrieved.
Default: 100
offset(number)

The upper indent when fetching sites. It is used to build the LIMIT part of the query.

For example, if you specify 10 here, and a total of 20 sites are found, the last 10 will be returned, and the top 10 will be skipped...
Default: 0

no_found_rows(logical)
Disable the count of found rows. Disables the SQL operator SQL_CALC_FOUND_ROWS.
Default: true
orderby(row|array)

Column by which to sort the retrieved sites. Can be:

site__in
network__in
domain
last_updated
path
registered
network_id
domain_length
path_length
id

You can also specify: false, array() or none to disable the ORDER BY part of the query.

Default: 'id'

order(string)
Sort direction: Can be: ASC (123, abv) or DESC (321, bva).
Default: 'ASC'
network_id(number)
The ID of the network whose sites you want to retrieve.
Default: (ID of the current network)
network__in(array)
An array of IDs of networks whose sites you want to retrieve.
Default: ''
network__not_in(array)
An array of IDs of networks whose sites you want to exclude.
Default: ''
domain(string)
The domain of the site you want to get.
Default: ''
domain__in(array)
The array of site domains to be retrieved.
Default: ''
domain__not_in(array)
An array of site domains to exclude.
Default: ''
path(string)
The path that the site should contain.
Default: ''
path__in(array)
An array of paths that can contain sites.
Default: ''
path__not_in(array)
An array of paths that sites cannot have.
Default: ''
public(number)
Limit sampling to public sites only. With 'public' status. Can be: 1 - only public sites or 0 - not public sites.
Default: null
archived(number)
Limit the selection to sites in the archive. Can be: 1 or 0.
Default: null
mature(number)
Limit selection to mature sites only. Can be: 1 or 0.
Default: null
spam(number)
Limit selection to sites with spam status. Can be: 1 or 0.
Default: null
deleted(number)
Limit the selection to deleted sites only. Can be: 1 or 0.
Default: null
The search query to search for sites.
Default: ''
search_columns(array)
An array of column names to search by, if the search parameter is set. Can be: domain or path.
Default: array()
lang_id(number) (since WP 4.8)
Limits the result to the language ID.
lang__in(array) (with WP 4.8)
Array of language IDs of the sites to be fetched.
lang__not_in(array) (with WP 4.8)
Array with language IDs of the sites you want to exclude from the result.
update_site_cache(logical)
Whether to reset the cache for the found sites if the cache for them is already installed.
Default: false
update_site_meta_cache(true/false) (c WP 5.1)
Should the metadata cache be updated for the found sites.
Default: true
meta_query(array) (c WP 5.1)
Adds a selection condition to the SQL query based on meta fields (metadata) of sites. See. WP_Meta_Query.
Default: []
meta_key(string) (c WP 5.1)
Get sites with the meta key specified here. Can be used in conjunction with $meta_value.
Default: ''
meta_value(string) (c WP 5.1)
Get sites with the meta value specified here. Usually used in conjunction with $meta_key.
Default: ''
meta_type(string) (c WP 5.1)
The data type to which the $meta_value column will be reduced for comparison. Possible variants: see. type in WP_Meta_Query.
Default: ''
meta_compare(string) (c WP 5.1)
A comparison statement for the value specified in $meta_value. Possible variants: see. compare in WP_Meta_Query.
Default: ''

Examples

0

#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

Changelog

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

get_sites() code WP 6.5.2

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

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