get_sites( string|array $args = array() ): array|int
Retrieves a list of sites matching requested arguments.
Contents
Description
See also
Parameters
-
$args
string|array Optional -
Array or string of arguments. See WP_Site_Query::__construct() for information on accepted arguments.
More Arguments from WP_Site_Query::__construct( ... $query )
Array or query string of site query parameters.
site__in
int[]Array of site IDs to include.site__not_in
int[]Array of site IDs to exclude.count
boolWhether to return a site count (true) or array of site objects.
Default false.date_query
arrayDate query clauses to limit sites by. See WP_Date_Query.
Default null.fields
stringSite fields to return. Accepts'ids'
(returns an array of site IDs) or empty (returns an array of complete site objects).ID
intA site ID to only return that site.number
intMaximum number of sites to retrieve. Default 100.offset
intNumber of sites to offset the query. Used to build LIMIT clause.
Default 0.no_found_rows
boolWhether to disable theSQL_CALC_FOUND_ROWS
query. Default true.orderby
string|arraySite status or array of statuses. Accepts:
'id'
'domain'
'path'
'network_id'
'last_updated'
'registered'
'domain_length'
'path_length'
'site__in'
'network__in'
'deleted'
'mature'
'spam'
'archived'
'public'
- false, an empty array, or
'none'
to disableORDER BY
clause.
'id'
.order
stringHow to order retrieved sites. Accepts'ASC'
,'DESC'
. Default'ASC'
.network_id
intLimit results to those affiliated with a given network ID. If 0, include all networks. Default 0.network__in
int[]Array of network IDs to include affiliated sites for.network__not_in
int[]Array of network IDs to exclude affiliated sites for.domain
stringLimit results to those affiliated with a given domain.domain__in
string[]Array of domains to include affiliated sites for.domain__not_in
string[]Array of domains to exclude affiliated sites for.path
stringLimit results to those affiliated with a given path.path__in
string[]Array of paths to include affiliated sites for.path__not_in
string[]Array of paths to exclude affiliated sites for.public
intLimit results to public sites. Accepts'1'
or'0'
.archived
intLimit results to archived sites. Accepts'1'
or'0'
.mature
intLimit results to mature sites. Accepts'1'
or'0'
.spam
intLimit results to spam sites. Accepts'1'
or'0'
.deleted
intLimit results to deleted sites. Accepts'1'
or'0'
.lang_id
intLimit results to a language ID.lang__in
string[]Array of language IDs to include affiliated sites for.lang__not_in
string[]Array of language IDs to exclude affiliated sites for.search
stringSearch term(s) to retrieve matching sites for.search_columns
string[]Array of column names to be searched. Accepts'domain'
and'path'
.
Default empty array.update_site_cache
boolWhether to prime the cache for found sites. Default true.update_site_meta_cache
boolWhether to prime the metadata cache for found sites. Default true.meta_key
string|string[]Meta key or keys to filter by.meta_value
string|string[]Meta value or values to filter by.meta_compare
stringMySQL operator used for comparing the meta value.
See WP_Meta_Query::__construct() for accepted values and default value.meta_compare_key
stringMySQL operator used for comparing the meta key.
See WP_Meta_Query::__construct() for accepted values and default value.meta_type
stringMySQL data type that the meta_value column will be CAST to for comparisons.
See WP_Meta_Query::__construct() for accepted values and default value.meta_type_key
stringMySQL data type that the meta_key column will be CAST to for comparisons.
See WP_Meta_Query::__construct() for accepted values and default value.meta_query
arrayAn associative array of WP_Meta_Query arguments.
See WP_Meta_Query::__construct() for accepted values.
Default:
array()
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/ms-site.php
.
View all references
function get_sites( $args = array() ) {
$query = new WP_Site_Query();
return $query->query( $args );
}
Changelog
Version | Description |
---|---|
4.8.0 | Introduced the 'lang_id' , 'lang__in' , and 'lang__not_in' parameters. |
4.6.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Also good to know… get_sites now returns an OBJECT not a named array.
A code example that may help:
Top ↑
Feedback
Please note that without specifying a ‘number’ attribute, this will only affect the first 100 sites! — By Stevish —
Beware, using
get_sites()
as a drop-in forwp_get_sites()
may not produce results as expected.PHP Fatal error: Cannot use object of type WP_Site as array in /path/to/code/that/uses/get_sites/method/file.php
It’s true that
get_sites()
returns an array, however, it produces an array of sites as objects. This is different fromwp_get_sites()
, which used to produce a multidimensional array of the sites, with their properties in a secondary array dimension (simply an array of site arrays with that site’s properties).If you’re attempting to loop through the sites to get the properties of each site with get_sites(), you’ll need to convert each site object to an array using
get_object_vars( object )
http://www.php.net/manual/en/function.get-object-vars.php.See the example below noting the use of get_object_vars on line three:
This should return the following list of sites:
Site ID/Name: 1 / SiteNameOne
Site ID/Name: 2 / SiteNameTwo
Site ID/Name: 3 / SiteNameThree
wp_get_sites() ‘limit’ argument is now ‘number’.
wp_get_sites() converted this to $args[‘number’] for you, get_sites() does not appear to handle this parameter name conversion for you.
Reference: https://developer.wordpress.org/reference/functions/wp_get_sites/
Top ↑
Feedback
I’m seeing public = -2 in the query return. This is contrary to what is expected, either a 0 or a 1 Why is that? — By ldigregorio —