WordPress at Your Fingertips

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.

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

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

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 5.8.1

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

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

Multisite

No comments
    Log In