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:1129
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 towpdb::$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
- Scaling
- Scales with input
- Instructions
- 5–59
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 106.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–16 | none |
!is_multisite() | 10–20 | is_multisite() |
!is_multisite() | 16–18 | array_merge(), is_multisite() |
is_multisite() | 17–27 | is_multisite(), array_merge() |
is_multisite() | 23–25 | array_merge(), is_multisite(), array_merge() |
| always | 27–48 | ->get_blog_prefix(), array_merge() |
!is_multisite() | 29–52 | is_multisite(), ->get_blog_prefix(), array_merge() |
!is_multisite() | 35–50 | array_merge(), is_multisite(), ->get_blog_prefix(), array_merge() |
is_multisite() | 36–59 | is_multisite(), array_merge(), ->get_blog_prefix(), array_merge() |
is_multisite() | 42–57 | array_merge(), is_multisite(), array_merge(), ->get_blog_prefix(), array_merge() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 106 | 5–59 | 18 | |
| 8.5 | 106 | 5–59 | 18 | |
| 8.4 | 106 | 5–59 | 18 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 109 | 5–59 | 18 | |
| 8.2 | 109 | 5–59 | 18 | |
| 8.1 | 109 | 5–59 | 18 | 1 more instruction than PHP 7.4 |
| 7.4 | 108 | 5–65 | 18 |
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
- is_multisite()Determines whether Multisite is enabled.
- wpdb::get_blog_prefix()Gets blog prefix.
Used by · 2
- wpdb::set_blog_id()Sets blog ID.
- wpdb::set_prefix()Sets the table prefix for the WordPress tables.
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.
Signature, return type and hooks compared across 5 parsed releases.
old now includes deprecated multisite global tables only on multisite.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.