How to Connect Another Database in WordPress

Recently, I received a question about how to connect one more 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 or manage any functionality from a single database. I believe we never need to connect more than one database to the WordPress website.

But still, if someone is looking for the solution on connecting multiple databases then there is one hack for it. Using this hack, you are able use the methods of the wpdb class to fetch the records from another database. This hack will not allow you to use the core methods provided by WordPress on your second database. The functions like get_option(), update_option(), wp_insert_post(), get_post_meta() will not be accessible for your other database.

In that case, the user can fetch the data from options, posts tables using SQL queries with the methods 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. Doing so, you get access to all available methods of wpdb class. The user can use these class methods directly to interact with the external database.

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

As said, it needs to 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.

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 the above code you are connected with another database. You can use this second instance as below:

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 must be the same as the original database. If your table prefix is different then you need to mention prefix 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 another database and get records from it on your WordPress website. But I recommend not to use multiple databases for your WordPress website. Try to achieve your goal from the single database only.

Related Articles

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

3 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 *