Access Another Database in WordPress
by c.bavota | Posted in Tutorials | 23 comments
Apr
04
2011
When accessing the database in WordPress, you need to use the global variable $wpdb. This will take the constants you set in wp-config.php to connect to your database and retrieve whichever results you specify. But what if you have a second database you wish to connect to for whatever reason? To do that all you need is the following snippet:
$newdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST); $newdb->show_errors();
Add the code snippet to your functions.php file or a specific page template, and then you can use $newdb just like you would $wpdb to access a second database and retrieve some data.



Great tip! I knew that there was an API to do that but never realized it was that easy!
That is a lovely tip. I can see how many different ways it can be used without messing up the default database for WP.
Absolutely, Thanks Bavotasan. I used your trick to access parts of the same database from two different sites. Works like a charm!
So simple yet so helpful. Thanks!
What would be a few real world examples that you’d use this for? Can you elaborate perhaps on a few thing’s you’ve done with it?
I’m currently working on a WordPress site that needs to access data from a second database to create some lists. This is the functions I am using.
Maybe I will use this tip in the future, thanks
Could this be done with an external database on a different domain as well?
Yup. I used it to gather information from a domain on a totally different server.
Thanks man for that tip, it is gonna be useful
Thanks for the tip.
Could this be used to pull in post from another WordPress site?
I want to build a kind of portal that displays posts from two existing sites of mine.
Yes it could. That is what I have used it for.
wrote about something similar back in my post http://www.davidtan.org/wordpress-how-to-use-wpdb-class-to-connect-fetch-data-from-another-database/ but didn’t know that can be inserted into functions.php too thanks for the tips
so it is possible to grab data from another server, or is it just on the same host?
You can do it from another server.
Great tip! Thanks!!
Do you have to change it back?
Nope. You are just setting a different variable with the new db info.
Can you give an example of how this would work with a custom query as well? I have two sites sharing a server but with separate DBs. I’d like to bring in the 3 latest posts from one DB (blog) into the 2nd. I’ve found a few examples online with this method but none of them seem to be working. Either I’m getting something wrong, or it’s not doing what I was hoping for.
TIA!
The easiest way would be to grab the RSS feed from the other blog. WordPress has a great function called
fetch_feed():http://codex.wordpress.org/Function_Reference/fetch_feed
Thanks, the only real issue I have with using RSS is styling things the way I want and importing an image as well.
Your the MAN! This helped so much, thank you
Regards
James