wppaste
WordPress

get_current_blog_id(): int

Since
3.1.0
Source
wp-includes/load.php:1481

Returns the ID of the site currently being served by reading the global $blog_id variable and passing it through absint(). On a single-site install this is normally 1, and on multisite it changes only after switch_to_blog() or restore_current_blog() runs. Pair it with get_current_network_id() when you need the network rather than the site, since the two are easy to confuse in multisite code.

Retrieves the current site ID.

Compatibility

WordPress
since 3.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Return value

int
Site ID.

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Print the current site ID

A minimal call that shows what the function returns on the running site.

$site_id = get_current_blog_id();

echo esc_html( 'Current site ID: ' . $site_id );

On a non-multisite install this will print 1.

Build a site-aware transient key for a cached post count

Use the site ID as part of a cache key so a caching function stays correct if the same code later runs in a multisite network.

$cache_key = 'publish_count_site_' . get_current_blog_id();

$counts = wp_count_posts();
set_transient( $cache_key, $counts->publish, HOUR_IN_SECONDS );

$cached_count = get_transient( $cache_key );

printf(
	'Cached publish count for site %d: %d',
	get_current_blog_id(),
	(int) $cached_count
);

Common problems and fixes · 4

Why does get_current_blog_id() always return 1 on my site?

On a single-site install the global $blog_id that this function reads is simply defined as 1, so the value is expected and correct, not a bug.

Why did I get 0 when I called this very early in a plugin?

The function only returns absint() of whatever the global $blog_id currently holds. If your code runs before WordPress sets that global, $blog_id is null and absint( null ) is 0.

How is this different from get_current_network_id()?

The two names look alike but return different things in a multisite network: get_current_blog_id() is the individual site, get_current_network_id() is the network that groups sites together.

Does the return value change when I call switch_to_blog()?

Yes. switch_to_blog() updates the global $blog_id that this function reads, so calling get_current_blog_id() again inside the switched context returns the other site's ID until restore_current_blog() is called.

Alternatives and related functions

get_current_network_id
When you need the ID of the multisite network rather than the individual site.
switch_to_blog
When you need to temporarily change which site's ID get_current_blog_id() reports and run code in that site's context.
get_blog_details
When you need more information about a site than just its numeric ID, such as its domain or path.
is_multisite
When your code needs to branch depending on whether the install is multisite at all before trusting a site ID.

Performance profile

How much work a call to get_current_blog_id() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5

Executed per call on PHP 8.5. The body compiles to 5.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_current_blog_id() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always5absint()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 5 instructions, 5 executed per call, 0 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

  • absint()Converts a value to non-negative integer.

Used by · 50

Show all 50

Source code

function get_current_blog_id() {	global $blog_id; 	return absint( $blog_id );}

Changelog

Introduced in 3.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/load.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.