Database Schema
Core Database Architecture
The wordpress project utilizes the standard WordPress relational schema, optimized for content management and extensibility. By default, the database consists of several core tables that manage everything from user authentication to content relationships.
Core Tables Overview
The following tables represent the primary data structures used within the application:
| Table Name | Description | Key Relationships |
| :--- | :--- | :--- |
| wp_posts | The central repository for all content (pages, posts, custom post types). | Parent to wp_postmeta. |
| wp_postmeta | Stores metadata associated with posts in a key-value format. | Linked to wp_posts via post_id. |
| wp_users | Stores user credentials and basic profile information. | Parent to wp_usermeta. |
| wp_terms | Manages taxonomies, categories, and tags. | Linked to wp_term_relationships. |
| wp_options | Stores site-wide settings and configuration data. | Independent. |
Custom Table Integration
For high-performance features or specialized data structures that do not fit the standard "Post Meta" model, custom tables are integrated. These tables are managed via the $wpdb global object.
Creating Custom Tables
When extending the schema, use the dbDelta function. This ensures that the table structure is updated safely without losing existing data.
global $wpdb;
$table_name = $wpdb->prefix . 'custom_analytics';
$charset_collate = $wpdb->get_charset_collate();
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
event_name tinytext NOT NULL,
user_id bigint(20) NOT NULL,
PRIMARY KEY (id)
) $charset_collate;";
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
Data Access Interface
Interaction with the database schema is abstracted through the $wpdb class. This is the primary public interface for executing queries.
Querying Data
To retrieve custom data from the schema, use the get_results or get_row methods. Always use prepare() to prevent SQL injection.
global $wpdb;
$user_id = 123;
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM {$wpdb->prefix}custom_analytics WHERE user_id = %d",
$user_id
)
);
foreach ( $results as $row ) {
echo $row->event_name;
}
Writing Data
For inserting or updating records, use the helper methods which handle the formatting and escaping automatically.
global $wpdb;
$wpdb->insert(
$wpdb->prefix . 'custom_analytics',
array(
'time' => current_time( 'mysql' ),
'event_name' => 'page_view',
'user_id' => get_current_user_id(),
),
array( '%s', '%s', '%d' ) // Data formats
);
Schema Configuration
The database configuration is managed via the wp-config.php file.
| Constant | Description |
| :--- | :--- |
| DB_NAME | The name of the database for WordPress. |
| DB_USER | MySQL database username. |
| DB_PASSWORD | MySQL database password. |
| DB_HOST | MySQL hostname (usually localhost). |
| $table_prefix | The prefix placed in front of table names (e.g., wp_). |
Note: For security and multi-tenant environments, always verify the
$table_prefixvia the global$wpdb->prefixproperty rather than hardcoding "wp_" in your queries.