WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have read here about switching the current wp database on the fly using


Once done, I understand that i will have to issue the same statement to the original database.


  • Right after changing the database, do i need to flush caches or similar stuff to get things working properly? Will I need to call again WP header?

  • Potential issues?

share|improve this question
I don't think select() will switch the db :) That's used to select tables. Read the question you linked - you need to instantiate a new wpdb object, with the db name as one of the arguments... – onetrickpony Jun 22 '11 at 10:15
It does. I'm testing this and seems working. As for the linked post, please read last posts from Matt & Dennis. – Riccardo Jun 22 '11 at 10:34
But how do you pass the user/password to connect to that database? – onetrickpony Jun 22 '11 at 10:44
I'm using the same user/password for both the databases. Works perfectly! – Riccardo Jun 22 '11 at 15:41
$wpdb->select() is used to switch to a different database on the same server. If your user doesn't have access, it won't work. It internally calls mysql_select_db() and passes in the database name. – EAMann Jun 22 '11 at 20:26
up vote 1 down vote accepted

Yes it works!

This is the code, assuming both the databases share the same username & password:


Once done, re-issue the same statements, with the original database name.

Can't see any performance issue at the moment, maybe because this is done when building a custom form picking data from two posts out of two databases. It may impact performance when used on a large amount of rows in batch loop...

share|improve this answer
Omg thank you! I've been going absolutely crazy trying to import users from an old WP DB and wp_insert_user kept saying "Username exists". wp_cache_flush was the answer. – powerbuoy Jun 9 at 8:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.