Opening a Second Database in WordPress

Posted by Dan Romanchik on Dec 26, 2011 in WordPress |

I am in the process of creating a WP site that is part of a family of WP sites. Each site has its own database, but I need to include information about posts on the secondary sites on the main site. To do this, I have to open the database of secondary site in question and grab some information about the posts on that site.

My first attempts at doing this were less than successful. So, I Googled around, and found a site with the following bit of code at batovasan.com:

$newdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST);
$newdb->show_errors();

I put these two lines in functions.php, and it works like a charm, after substituting the right values for the wpdb function parameters, of course. Remember that you have to declare $newdb to be a global variable in the function that you’re going to use it.

As I was writing this, it occurred to me that I could also do this by accessing the RSS feed of the second site. I’m guessing that this is a little more efficient, though.

Reply

You must be logged in to post a comment.

Copyright © 2012 Web Publishing Group All rights reserved.
Desk Mess Mirrored v1.0.8 theme from BuyNowShop.com.

Switch to our mobile site