Title: get_sites
Published: August 16, 2016
Last modified: August 20, 2026

---

# get_sites( string|array $args = array() ): 󠀁[WP_Site](https://developer.wordpress.org/reference/classes/wp_site/)󠁿[]|int[]|int

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#description)
    - [See also](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#see-also)
 * [Parameters](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#wp--skip-link--target)

Retrieves a list of sites matching requested arguments.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#description)󠁿

### 󠀁[See also](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#see-also)󠁿

 * [WP_Site_Query::parse_query()](https://developer.wordpress.org/reference/classes/WP_Site_Query/parse_query/)

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#parameters)󠁿

 `$args`string|arrayoptional

Array or string of arguments. See [WP_Site_Query::__construct()](https://developer.wordpress.org/reference/classes/wp_site_query/__construct/)
for information on accepted arguments.

Default:`array()`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#return)󠁿

 [WP_Site](https://developer.wordpress.org/reference/classes/wp_site/)[]|int[]|int
List of [WP_Site](https://developer.wordpress.org/reference/classes/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](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#source)󠁿

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

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/ms-site.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/ms-site.php#L445)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/ms-site.php#L445-L449)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Site_Query::__construct()](https://developer.wordpress.org/reference/classes/wp_site_query/__construct/)`wp-includes/class-wp-site-query.php` |

Sets up the site query, based on the query vars passed.

  |

| Used by | Description | 
| [get_oembed_response_data_for_url()](https://developer.wordpress.org/reference/functions/get_oembed_response_data_for_url/)`wp-includes/embed.php` |

Retrieves the oEmbed response data for a given URL.

  | 
| [WP_Network::get_main_site_id()](https://developer.wordpress.org/reference/classes/wp_network/get_main_site_id/)`wp-includes/class-wp-network.php` |

Returns the main site ID for the network.

  | 
| [WP_MS_Sites_List_Table::prepare_items()](https://developer.wordpress.org/reference/classes/wp_ms_sites_list_table/prepare_items/)`wp-admin/includes/class-wp-ms-sites-list-table.php` |

Prepares the list of sites for display.

  | 
| [WP_Importer::set_blog()](https://developer.wordpress.org/reference/classes/wp_importer/set_blog/)`wp-admin/includes/class-wp-importer.php` |

Sets the blog to import to.

  | 
| [get_blogs_of_user()](https://developer.wordpress.org/reference/functions/get_blogs_of_user/)`wp-includes/user.php` |

Gets the sites a user belongs to.

  | 
| [wp_get_sites()](https://developer.wordpress.org/reference/functions/wp_get_sites/)`wp-includes/ms-deprecated.php` |

Return an array of sites for a network or networks.

  | 
| [wp_update_network_site_counts()](https://developer.wordpress.org/reference/functions/wp_update_network_site_counts/)`wp-includes/ms-functions.php` |

Updates the network-wide site count.

  | 
| [domain_exists()](https://developer.wordpress.org/reference/functions/domain_exists/)`wp-includes/ms-functions.php` |

Checks whether a site name is already taken.

  | 
| [get_blog_id_from_url()](https://developer.wordpress.org/reference/functions/get_blog_id_from_url/)`wp-includes/ms-functions.php` |

Gets a blog’s numeric ID from its URL.

  | 
| [get_site_by_path()](https://developer.wordpress.org/reference/functions/get_site_by_path/)`wp-includes/ms-load.php` |

Retrieves the closest matching site object by its domain and path.

  | 
| [get_id_from_blogname()](https://developer.wordpress.org/reference/functions/get_id_from_blogname/)`wp-includes/ms-blogs.php` |

Retrieves a site’s ID given its (subdomain or directory) slug.

  |

[Show 6 more](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.8.0](https://developer.wordpress.org/reference/since/4.8.0/) | Introduced the `'lang_id'`, `'lang__in'`, and `'lang__not_in'` parameters. | 
| [4.6.0](https://developer.wordpress.org/reference/since/4.6.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 5 content](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#comment-content-1842)
 2.    [Lance Cleveland](https://profiles.wordpress.org/charlestonsw/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/get_sites/#comment-1842)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%23comment-1842)
     Vote results for this note: 9[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%23comment-1842)
 4.  Also good to know… get_sites now returns an OBJECT not a named array.
 5.  A code example that may help:
 6.      ```php
         // WordPress 4.6
         //
         if ( function_exists( 'get_sites' ) && class_exists( 'WP_Site_Query' ) ) {
         	$sites = get_sites();
         	foreach ( $sites as $site ) {
         		switch_to_blog( $site-&gt;blog_id );
                         // do something
         		restore_current_blog();
         	}
         	return;
         }
     
         // WordPress &lt; 4.6
         //
         if ( function_exists( 'wp_get_sites' ) ) {
         	$sites = wp_get_sites();
         	foreach ( $sites as $site ) {
         		switch_to_blog( $site['blog_id'] );
                         // do something
         		restore_current_blog();
         	}
         	return;
         }
         ```
     
 7.   * Please note that without specifying a ‘number’ attribute, this will only affect
        the first 100 sites!
      * [Stevish](https://profiles.wordpress.org/stevish/) [8 years ago](https://developer.wordpress.org/reference/functions/get_sites/#comment-3129)
 8.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%3Freplytocom%3D1842%23feedback-editor-1842)
 9.   [Skip to note 6 content](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#comment-content-1817)
 10.   [mkormendy](https://profiles.wordpress.org/mkormendy/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/get_sites/#comment-1817)
 11. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%23comment-1817)
     Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%23comment-1817)
 12. Beware, using `get_sites()` as a drop-in for `wp_get_sites()` may not produce 
     results as expected.
 13. `PHP Fatal error: Cannot use object of type WP_Site as array in /path/to/code/
     that/uses/get_sites/method/file.php`
 14. It’s true that `get_sites()` returns an array, **_however_**, it produces an array
     of sites as objects. This is different from `wp_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).
 15. If you’re attempting to loop through the sites to get the properties of each site
     with [get_sites()](https://developer.wordpress.org/reference/functions/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](http://www.php.net/manual/en/function.get-object-vars.php).
 16. See the example below noting the use of get_object_vars on line three:
 17.     ```php
         $subsites = get_sites();
         foreach( $subsites as $subsite ) {
           $subsite_id = get_object_vars($subsite)["blog_id"];
           $subsite_name = get_blog_details($subsite_id)->blogname;
           echo 'Site ID/Name: ' . $subsite_id . ' / ' . $subsite_name . '\n';
         }
         ```
     
 18. This should return the following list of sites:
 19. Site ID/Name: 1 / SiteNameOne
      Site ID/Name: 2 / SiteNameTwo Site ID/Name: 3 /
     SiteNameThree
 20.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%3Freplytocom%3D1817%23feedback-editor-1817)
 21.  [Skip to note 7 content](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#comment-content-1809)
 22.   [Lance Cleveland](https://profiles.wordpress.org/charlestonsw/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/get_sites/#comment-1809)
 23. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%23comment-1809)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%23comment-1809)
 24. [wp_get_sites()](https://developer.wordpress.org/reference/functions/wp_get_sites/)‘
     limit’ argument is now ‘number’.
 25. [wp_get_sites()](https://developer.wordpress.org/reference/functions/wp_get_sites/)
     converted this to $args[‘number’] for you, [get_sites()](https://developer.wordpress.org/reference/functions/get_sites/)
     does not appear to handle this parameter name conversion for you.
 26. Reference: [https://developer.wordpress.org/reference/functions/wp_get_sites/](https://developer.wordpress.org/reference/functions/wp_get_sites/)
 27.  * 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?
      * [ldigregorio](https://profiles.wordpress.org/ldigregorio/) [7 years ago](https://developer.wordpress.org/reference/functions/get_sites/#comment-3536)
 28.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%3Freplytocom%3D1809%23feedback-editor-1809)
 29.  [Skip to note 8 content](https://developer.wordpress.org/reference/functions/get_sites/?output_format=md#comment-content-6876)
 30.   [Clicknathan](https://profiles.wordpress.org/clicknathan/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/get_sites/#comment-6876)
 31. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%23comment-6876)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%23comment-6876)
 32. The number of sites returned is limited to 100 by default. you can increase this
     using `number`.
 33.     ```php
         $args = array(
         	'number' => 420,
         );
         $blog_list = get_sites( $args );
         ```
     
 34. Note that -1 (often used to “get all”) does not appear to work here.
 35.  * You can use `'number' => 0` to get all sites.
      * [rutger.gabriel](https://profiles.wordpress.org/rutgergabriel/) [8 months ago](https://developer.wordpress.org/reference/functions/get_sites/#comment-7479)
      * Or set `number` to 0 for unlimited.
      * [vee](https://profiles.wordpress.org/okvee/) [6 months ago](https://developer.wordpress.org/reference/functions/get_sites/#comment-7492)
 36.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F%3Freplytocom%3D6876%23feedback-editor-6876)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fget_sites%2F)
before being able to contribute a note or feedback.