wppaste
WordPress

wpdb::tables( string $scope = 'all', bool $prefix = true, int $blog_id = 0 ): string[]

Since
3.0.0, 6.1.0
Source
wp-includes/class-wpdb.php:1128
Returns an array of WordPress tables.

Description

Also allows for the CUSTOM_USER_TABLE and CUSTOM_USER_META_TABLE to override the WordPress users and usermeta tables that would otherwise be determined by the prefix.

The $scope argument can take one of the following:

  • 'all' - returns 'all' and 'global' tables. No old tables are returned.
  • 'blog' - returns the blog-level tables for the queried blog.
  • 'global' - returns the global tables for the installation, returning multisite tables only on multisite.
  • 'ms_global' - returns the multisite global tables, regardless if current installation is multisite.
  • 'old' - returns tables which are deprecated.

Compatibility

WordPress
since 6.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.

Parameters

$scopestringoptional
Possible values include 'all', 'global', 'ms_global', 'blog', or 'old' tables. Default 'all'.Default: 'all'
$prefixbooloptional
Whether to include table prefixes. If blog prefix is requested, then the custom users and usermeta tables will be mapped. Default true.Default: true
$blog_idintoptional
The blog_id to prefix. Used only when prefix is requested.
Defaults to wpdb::$blogid.Default: 0

Return value

string[]
Table names. When a prefix is requested, the key is the unprefixed table name.

Performance profile

How much work a call to wpdb::tables() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
5–59

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 106.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

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

What one call costs · 10 distinct outcomes

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

WhenInstructionsCalls it makes
always5–16none
!is_multisite()10–20is_multisite()
!is_multisite()16–18array_merge(), is_multisite()
is_multisite()17–27is_multisite(), array_merge()
is_multisite()23–25array_merge(), is_multisite(), array_merge()
always27–48->get_blog_prefix(), array_merge()
!is_multisite()29–52is_multisite(), ->get_blog_prefix(), array_merge()
!is_multisite()35–50array_merge(), is_multisite(), ->get_blog_prefix(), array_merge()
is_multisite()36–59is_multisite(), array_merge(), ->get_blog_prefix(), array_merge()
is_multisite()42–57array_merge(), is_multisite(), array_merge(), ->get_blog_prefix(), array_merge()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev1065–5918
8.51065–5918
8.41065–59183 fewer instructions than PHP 8.3
8.31095–5918
8.21095–5918
8.11095–59181 more instruction than PHP 7.4
7.41085–6518

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 · 2

Used by · 2

Source code

	public function tables( $scope = 'all', $prefix = true, $blog_id = 0 ) {		switch ( $scope ) {			case 'all':				$tables = array_merge( $this->global_tables, $this->tables );				if ( is_multisite() ) {					$tables = array_merge( $tables, $this->ms_global_tables );				}				break;			case 'blog':				$tables = $this->tables;				break;			case 'global':				$tables = $this->global_tables;				if ( is_multisite() ) {					$tables = array_merge( $tables, $this->ms_global_tables );				}				break;			case 'ms_global':				$tables = $this->ms_global_tables;				break;			case 'old':				$tables = $this->old_tables;				if ( is_multisite() ) {					$tables = array_merge( $tables, $this->old_ms_global_tables );				}				break;			default:				return array();		} 		if ( $prefix ) {			if ( ! $blog_id ) {				$blog_id = $this->blogid;			}			$blog_prefix   = $this->get_blog_prefix( $blog_id );			$base_prefix   = $this->base_prefix;			$global_tables = array_merge( $this->global_tables, $this->ms_global_tables );			foreach ( $tables as $k => $table ) {				if ( in_array( $table, $global_tables, true ) ) {					$tables[ $table ] = $base_prefix . $table;				} else {					$tables[ $table ] = $blog_prefix . $table;				}				unset( $tables[ $k ] );			} 			if ( isset( $tables['users'] ) && defined( 'CUSTOM_USER_TABLE' ) ) {				$tables['users'] = CUSTOM_USER_TABLE;			} 			if ( isset( $tables['usermeta'] ) && defined( 'CUSTOM_USER_META_TABLE' ) ) {				$tables['usermeta'] = CUSTOM_USER_META_TABLE;			}		} 		return $tables;	}

Changelog

Introduced in 3.0.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.

6.1.0
old now includes deprecated multisite global tables only on multisite.from the docblock
3.0.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.9.7 tag, from src/wp-includes/class-wpdb.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.