How to Connect Another Database in WordPress

Recently, I received a question from one of the readers about how to connect more than one database to the WordPress website. First, I want to make it clear WordPress is built in a way that you can run any number of sites(a multisite feature) without connecting multiple databases. From WordPress, you can even build or manage any functionality from a single database. I believe you never need to connect multiple databases to the WordPress website.

However, if someone is still looking for a solution for connecting multiple databases then one hack is available. Using this hack, you can connect another database using the wpdb class and use its methods to interact with the database. This hack will not allow you to use the core methods provided by WordPress on your second database. Methods like get_option(), update_option(), wp_insert_post(), get_post_meta(), etc. will not be accessible for your other database.

In that case, the user can fetch the data from options, and posts tables using SQL queries with the functions of wpdb class. These methods are get_var(), get_row(), get_col(), get_results(), etc.

Connect Another Database in WordPress

When you need to connect another database, create an instance of the wpdb class. After this, you get access to all available methods of wpdb class. The user can apply wpdb class methods directly against the external database.

For getting started, you should have the credentials of an external database.

When you create an instance of wpdb class, this instance should be available throughout the WordPress application. For this, add the below code in functions.php file. This code is executed whenever WordPress is initialized.

function connect_another_db() {
    global $seconddb;
    $seconddb = new wpdb(USERNAME, PASSWORD, DATABASE_NAME, HOSTNAME);
}
add_action('init', 'connect_another_db');

Make sure to replace all placeholders with the actual values. Upon adding this code, you are connected to another database. You can use this second instance as follows.

global $seconddb;
$user_count = $seconddb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
echo "<p>User count is {$user_count}</p>";

Keep a note your table prefix of another database should be the same as the original database. If your table prefix is different then you need to mention it explicitly as shown below.

global $seconddb;
$prefix = 'wp2_'; // here 'wp2' is the table prefix of second database
$user_count = $seconddb->get_var( "SELECT COUNT(*) FROM $prefix"."users" );
echo "<p>User count is {$user_count}</p>";

Using the above hacks you can connect to another database and interact with it on your WordPress website. But I recommend don’t use multiple databases for your WordPress website. Try to achieve your goal from a single database only.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

5 thoughts on “How to Connect Another Database in WordPress

  1. Hello @Sajid ! First thank you for sharing your function.
    Once your function is installed in functions.php file, how can I use it? Knowing that I get reviews like this:
    $reviews = new WP_Query( $tepeek_controller->args );
    A clue? thank you in advance!
    Georges

Leave a Reply

Your email address will not be published. Required fields are marked *