Take the 2-minute tour ×

In one of my page I have to fetch data from remote mysql database. We have connection to remote mysql database by creating new instance of $wpdb class.

$mydb = new wpdb(DB_USER2, DB_PASSWORD2, DB_NAME2, DB_HOST2);

$post_author=$mydb->get_row("Select display_name from wp_users where ID=1");

The above query is working fine. But when we are using get_the_author_meta or the_author_meta it will not return /print anything. This is same for every wordpress built in function.

Can anyone tell why built in wordpress function is not working when we connecting to remote database?

share|improve this question
Can I ask why you are doing this? –  Tom J Nowell Sep 24 '12 at 12:15

2 Answers 2

The WordPress APIs rely on the global $wpdb object, you can define any arbitrary object of type wpdb but that doesn't mean WordPress will use it.

Instead, something akin to the following might be better:

global $wpdb;
$tempDB = $wpdb;
$wpdb = new wpdb(DB_USER2, DB_PASSWORD2, DB_NAME2, DB_HOST2);
// do your remote SQL stuff

// perhaps a WP_Query loop here?

// we're done, now restore the default and cleanup
$wpdb = $tempDB;

I would advise though that there's very, very few instances were connecting to a second DB with a WordPress install is the optimum thing to do, there are many, many better ways of doing things. Multisite or RSS feed aggregation would probably be a lot better for you.

share|improve this answer

Is the second database a wordpress install? I ask because the functions you are wanting to use are specific to wordpress AND a wordpress DB, so if you are accessing a "regular" (non-wordpress) MySQL database you would be bound to have problems.

I have used other databases concurrently with a wordpress one with little difficulty, in situations where I wanted to bring data from elsewhere into a wordpress page, or write data elsewhere from a wordpress page. But you need to use straight-up php for getting/setting the info you want.

share|improve this answer
Yes, second database also wordpress install and I want to fetch the data from this remote mysql wordpress database using wordpress function. –  soumendu Sep 25 '12 at 6:49

Your Answer


