wp_get_sites( array $args = array() )

Return an array of sites for a network or networks.


Description #


Parameters #

$args

(array) (Optional) Array of default arguments. Optional.

  • 'network_id'
    (int|array) A network ID or array of network IDs. Set to null to retrieve sites from all networks. Defaults to current network ID.
  • 'public'
    (int) Retrieve public or non-public sites. Default null, for any.
  • 'archived'
    (int) Retrieve archived or non-archived sites. Default null, for any.
  • 'mature'
    (int) Retrieve mature or non-mature sites. Default null, for any.
  • 'spam'
    (int) Retrieve spam or non-spam sites. Default null, for any.
  • 'deleted'
    (int) Retrieve deleted or non-deleted sites. Default null, for any.
  • 'limit'
    (int) Number of sites to limit the query to. Default 100.
  • 'offset'
    (int) Exclude the first x sites. Used in combination with the $limit parameter. Default 0.

Default value: array()


Top ↑

Return #

(array) An empty array if the install is considered "large" via wp_is_large_network(). Otherwise, an associative array of site data arrays, each containing the site (network) ID, blog ID, site domain and path, dates registered and modified, and the language ID. Also, boolean values for whether the site is public, archived, mature, spam, and/or deleted.


Top ↑

Source #

File: wp-includes/ms-functions.php

function wp_get_sites( $args = array() ) {
	global $wpdb;

	if ( wp_is_large_network() )
		return array();

	$defaults = array(
		'network_id' => $wpdb->siteid,
		'public'     => null,
		'archived'   => null,
		'mature'     => null,
		'spam'       => null,
		'deleted'    => null,
		'limit'      => 100,
		'offset'     => 0,
	);

	$args = wp_parse_args( $args, $defaults );

	$query = "SELECT * FROM $wpdb->blogs WHERE 1=1 ";

	if ( isset( $args['network_id'] ) && ( is_array( $args['network_id'] ) || is_numeric( $args['network_id'] ) ) ) {
		$network_ids = implode( ',', wp_parse_id_list( $args['network_id'] ) );
		$query .= "AND site_id IN ($network_ids) ";
	}

	if ( isset( $args['public'] ) )
		$query .= $wpdb->prepare( "AND public = %d ", $args['public'] );

	if ( isset( $args['archived'] ) )
		$query .= $wpdb->prepare( "AND archived = %d ", $args['archived'] );

	if ( isset( $args['mature'] ) )
		$query .= $wpdb->prepare( "AND mature = %d ", $args['mature'] );

	if ( isset( $args['spam'] ) )
		$query .= $wpdb->prepare( "AND spam = %d ", $args['spam'] );

	if ( isset( $args['deleted'] ) )
		$query .= $wpdb->prepare( "AND deleted = %d ", $args['deleted'] );

	if ( isset( $args['limit'] ) && $args['limit'] ) {
		if ( isset( $args['offset'] ) && $args['offset'] )
			$query .= $wpdb->prepare( "LIMIT %d , %d ", $args['offset'], $args['limit'] );
		else
			$query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] );
	}

	$site_results = $wpdb->get_results( $query, ARRAY_A );

	return $site_results;
}


Top ↑

Changelog #

Changelog
Version Description
3.7.0 Introduced.

Top ↑

More Information #

If wp_is_large_network() returns TRUE, wp_get_sites() will return an empty array. By default wp_is_large_network() returns TRUE if there are 10,000 or more sites in your network. This can be filtered using the wp_is_large_network filter.

Each site’s array is composed entirely of string values, even for numeric values. This means that == or !=, not === or !==, should be used to compare [blog_id] to get_current_blog_id(), which returns an integer value.



Top ↑

User Contributed Notes #

  1. Skip to note content
    Contributed by Codex

    Basic Example

    if you specify null to the ‘network_id’ then all site infomation in the network are returned.

    <?php 
    $args = array(
        'network_id' => null,
        'public'     => null,
        'archived'   => null,
        'mature'     => null,
        'spam'       => null,
        'deleted'    => null,
        'limit'      => 100,
        'offset'     => 0,
    ); 
    $array = wp_get_sites( $args );
    print_r ($array);
    ?>
    

    Example of the array returned:

    Array(
        [0] => Array(
            [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] => Array(
            [blog_id] => 2
            [site_id] => 1
            [domain] => example.com
            [path] => /examplesubsite/
            [registered] => 2013-11-08 18:07:22
            [last_updated] => 2013-11-08 18:13:40
            [public] => 1
            [archived] => 0
            [mature] => 0
            [spam] => 0
            [deleted] => 0
            [lang_id] => 0
        )
    )
    

    If you specified ‘1’ to the ‘network_id’ then the array that include only the first element must be returned.

You must log in before being able to contribute a note.