On this page
WP_Site::get_instance( int $site_id ): WP_Site|false
Retrieves a site from the database by its ID.
Parameters
$site_idint Required-
The ID of the site to retrieve.
Return
WP_Site|false The site's object if found. False if not.
Source
File: wp-includes/class-wp-site.php. View all references
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 );
}
Related
Uses
| Uses | Description |
|---|---|
| WP_Site::__construct() wp-includes/class-wp-site.php | Creates a new WP_Site object. |
| wp_cache_add() wp-includes/cache.php | Adds data to the cache, if the cache key doesn’t already exist. |
| wpdb::get_row() wp-includes/class-wpdb.php | Retrieves one row from the database. |
| wp_cache_get() wp-includes/cache.php | Retrieves the cache contents from the cache by key and group. |
| wpdb::prepare() wp-includes/class-wpdb.php | Prepares a SQL query for safe execution. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
Used By
| Used By | Description |
|---|---|
| WP_MS_Sites_List_Table::site_states() wp-admin/includes/class-wp-ms-sites-list-table.php | Maybe output comma-separated site states. |
| get_site() wp-includes/ms-site.php | Retrieves site data given a site ID or site object. |
| get_blog_details() wp-includes/ms-blogs.php | Retrieve the details for a blog from the blogs table and blog options. |
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_site/get_instance