WordPress Version: 4.4
/**
* Return an array of sites for a network or networks.
*
* @since 3.7.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $args {
* Array of default arguments. Optional.
*
* @type int|array $network_id A network ID or array of network IDs. Set to null to retrieve sites
* from all networks. Defaults to current network ID.
* @type int $public Retrieve public or non-public sites. Default null, for any.
* @type int $archived Retrieve archived or non-archived sites. Default null, for any.
* @type int $mature Retrieve mature or non-mature sites. Default null, for any.
* @type int $spam Retrieve spam or non-spam sites. Default null, for any.
* @type int $deleted Retrieve deleted or non-deleted sites. Default null, for any.
* @type int $limit Number of sites to limit the query to. Default 100.
* @type int $offset Exclude the first x sites. Used in combination with the $limit parameter. Default 0.
* }
* @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.
*/
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;
}