WP_Site::get_instance() WordPress Method

The WP_Site::get_instance() method is used to retrieve the current site object. This is useful for retrieving information about the current site, such as its name or URL.

WP_Site::get_instance( int $site_id ) #

Retrieves a site from the database by its ID.


Parameters

$site_id

(int)(Required)The ID of the site to retrieve.


Top ↑

Return

(WP_Site|false) The site's object if found. False if not.


Top ↑

Source

File: wp-includes/class-wp-site.php

	public static function get_instance( $site_id ) {
		global $wpdb;

		$site_id = (int) $site_id;
		if ( ! $site_id ) {
			return false;
		}

		$_site = wp_cache_get( $site_id, 'sites' );

		if ( false === $_site ) {
			$_site = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->blogs} WHERE blog_id = %d LIMIT 1", $site_id ) );

			if ( empty( $_site ) || is_wp_error( $_site ) ) {
				$_site = -1;
			}

			wp_cache_add( $site_id, $_site, 'sites' );
		}

		if ( is_numeric( $_site ) ) {
			return false;
		}

		return new WP_Site( $_site );
	}


Top ↑

Changelog

Changelog
VersionDescription
4.5.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.