The require_wp_db WordPress PHP function loads the database class file and instantiates the $wpdb global.
Usage
require_wp_db();
Parameters
- None
 
More information
See WordPress Developer Resources: require_wp_db
Examples
Accessing the database to retrieve a list of post titles
// Load the database class
require_wp_db();
// Use the global $wpdb object
global $wpdb;
// Query to get post titles
$results = $wpdb->get_results("SELECT post_title FROM $wpdb->posts WHERE post_status = 'publish'");
// Loop through and display post titles
foreach ($results as $result) {
    echo $result->post_title . '<br>';
}
Inserting data into a custom table
// Load the database class
require_wp_db();
// Use the global $wpdb object
global $wpdb;
// Custom table name
$table_name = $wpdb->prefix . 'my_custom_table';
// Data to insert
$data = array(
    'name' => 'John Doe',
    'email' => '[email protected]'
);
// Insert data into the custom table
$wpdb->insert($table_name, $data);
Updating data in the wp_options table
// Load the database class
require_wp_db();
// Use the global $wpdb object
global $wpdb;
// Update the site name
$wpdb->update(
    $wpdb->options,
    array('option_value' => 'New Site Name'),
    array('option_name' => 'blogname')
);
Deleting rows from a custom table
// Load the database class require_wp_db(); // Use the global $wpdb object global $wpdb; // Custom table name $table_name = $wpdb->prefix . 'my_custom_table'; // Delete rows with email [email protected] $wpdb->delete($table_name, array('email' => '[email protected]'));
Executing a custom SQL query
// Load the database class
require_wp_db();
// Use the global $wpdb object
global $wpdb;
// Custom SQL query
$sql = "ALTER TABLE {$wpdb->prefix}my_custom_table ADD COLUMN age INT(3) NOT NULL";
// Execute the custom SQL query
$wpdb->query($sql);