Multisite Improvements with WordPress wp_get_sites() function

wp_get_sites(), is a long-awaited replacement for get_blog_list(). This function allows developers to easily get an array of all the sites on your network without resorting to a direct database query — just one of many improvements to multisite in latest version of WordPress.  wp_get_sites is located in wp-includes/ms-functions.php.

Parameters:
  • (array) $args { Array of default arguments. Optional.
Returns:
  • (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.

This function return an array of sites for a network or networks.

Source [ WordPress 3.8-alpha-26127 ]

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;
}

Returns an array containing the sites in the network, or an empty array on failure. Example of the array returned you can see in the above example code.

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.

If you are looking for wp_get_sites alternative, returning random sites, you can find the solution on this link: wp_get_sites alternative, returning random sites

Disclosure: Some of the links in this article are affiliate links and we may earn a small commission if you make a purchase, which helps us to keep delivering quality content to you. Here is our disclosure policy.

Noor Mustafa Raza
Noor Mustafa Raza
I am a WordPress Developer and Designer, author @WPArena. I am providing Free WordPress consultation and can help you to install WordPress in a secure way to small businesses and bloggers.

LEAVE A REPLY

Please enter your comment!
Please enter your name here
Captcha verification failed!
CAPTCHA user score failed. Please contact us!

spot_img

Related Articles

How to Avoid Common Mistakes That Can Impact Your WordPress Blog

A WordPress blog starting is quite exciting, but certain mistakes can prevent it from attaining the top ranking in search...
Read More
How would you react if you knew that you could design and style your theme without disturbing your parent theme?...
The most successful businesses keep themselves updated with the data of their market campaigns. When tracking visitors and analyzing visitor...