get_site() │ WP 4.6.0

Retrieves site data from a network by its ID. If no argument is specified, retrieves the current site.

The result is stored in the cache and passed through the get_site filter.

Use get_sites() to retrieve a list of sites by criteria.

The function is not defined in a regular (non-multisite) installation. Calling it causes a fatal error:

Fatal error: Uncaught Error: Call to undefined function get_site()
Hooks from the function

Returns

WP_Site|null.

  • WP_Site - site object.
  • null - if a site with the specified ID is not found.

Usage

get_site( $site );
$site(WP_Site|int|null)
Site object or ID. If the value is empty, the current site's ID is used.
Default: null

Examples

#1 Get the current site's data

$site = get_site();

/*
$site = WP_Site Object
(
	[blog_id] => 1
	[domain] => exmaple.com
	[path] => /
	[site_id] => 1
	[registered] => 2018-07-11 10:49:32
	[last_updated] => 2026-09-23 12:21:20
	[public] => 1
	[archived] => 0
	[mature] => 0
	[spam] => 0
	[deleted] => 0
	[lang_id] => 0
)
*/

#2 Get a site by ID

$site = get_site( 3 );

if ( $site ) {
	echo esc_html( $site->path ); //> /subsite
}

Changelog

Since 4.6.0 Introduced.

get_site() code WP 7.1.3

function get_site( $site = null ) {
	if ( empty( $site ) ) {
		$site = get_current_blog_id();
	}

	if ( $site instanceof WP_Site ) {
		$_site = $site;
	} elseif ( is_object( $site ) ) {
		$_site = new WP_Site( $site );
	} else {
		$_site = WP_Site::get_instance( $site );
	}

	if ( ! $_site ) {
		return null;
	}

	/**
	 * Fires after a site is retrieved.
	 *
	 * @since 4.6.0
	 *
	 * @param WP_Site $_site Site data.
	 */
	$_site = apply_filters( 'get_site', $_site );

	return $_site;
}