wp_count_sites() WordPress Function

The wp_count_sites() function is used to count the number of sites in a WordPress network. This function is only available in WordPress 3.0 or higher.

wp_count_sites( int $network_id = null ) #

Count number of sites grouped by site status.


Parameters

$network_id

(int)(Optional) The network to get counts for. Default is the current network ID.

Default value: null


Top ↑

Return

(int[]) Numbers of sites grouped by site status.

  • 'all'
    (int) The total number of sites.
  • 'public'
    (int) The number of public sites.
  • 'archived'
    (int) The number of archived sites.
  • 'mature'
    (int) The number of mature sites.
  • 'spam'
    (int) The number of spam sites.
  • 'deleted'
    (int) The number of deleted sites.


Top ↑

Source

File: wp-includes/ms-blogs.php

function wp_count_sites( $network_id = null ) {
	if ( empty( $network_id ) ) {
		$network_id = get_current_network_id();
	}

	$counts = array();
	$args   = array(
		'network_id'    => $network_id,
		'number'        => 1,
		'fields'        => 'ids',
		'no_found_rows' => false,
	);

	$q             = new WP_Site_Query( $args );
	$counts['all'] = $q->found_sites;

	$_args    = $args;
	$statuses = array( 'public', 'archived', 'mature', 'spam', 'deleted' );

	foreach ( $statuses as $status ) {
		$_args            = $args;
		$_args[ $status ] = 1;

		$q                 = new WP_Site_Query( $_args );
		$counts[ $status ] = $q->found_sites;
	}

	return $counts;
}


Top ↑

Changelog

Changelog
VersionDescription
5.3.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.