I have read about overwriting global variable $wpdb and connecting to a second database this way ( Search another database with wp_query using new wpdb ) so one could e.g. use WP_Query on 2nd DB. After that $wpdb is "restored" with $wpdb_backup.

See code below from @Tom J Nowell

global $wpdb;
// backup wpdb
$wpdb_backup = $wpdb;

// new db
$newdb = new wpdb($user, $password, $db, $host);

// pretend newdb is actually wpdb
$wpdb = $newdb;

// do stuff

// reset wpdb back
$wpdb = $wpdb_backup;

It's a theoretical question, maybe independant of hooks, only focused on execution-flow. Will PHP (or WP engine in general) primary execute and finish this script-part, where $wpdb's parameters are set to 2nd database's connection details and after doing stuff on 2nd DB will it later switch back to old $wpdb ($wpdb_backup) before a second website visitor clicks a post link right before $wpdb is changed (or any other possible action handling DB1) and PHP reacts after $wpdb is changed to $newdb?

If so, in this scenario, WP displays a post from DB2 though the visitor expects (well, it's probably less him - it's WP) the certain Post from DB1. If such a post does not exist in DB2 WP throws an error.

Am I wrong? Thank you for your explanations.

edit Is it about priority with hooks?

I know there is add_action ( 'hook_name', 'your_function_name', [priority], [accepted_args] ); for hook priority.

edit2 Short question: Does WP execute one action after another or does it handle them simultaneously (so that there can be conflicts manipulating $wpdb and after doing some stuff change it back)?

1 Answer 1


I am not sure if I understand your question right but you seem to misunderstand a bit how PHP requests are typically executed.

$wpdb is not persistent, it belongs to specific page load and only exists while that page load is being performed. Unless your code always changes it - independent second (third, etc) page load will have its own instance of database connection.

Same applies to actions - they are executed in the context of specific page load, not in any kind of global persistent way.

  • Ok, I get that. but $wpdb is a global variable, right? So if I set new parameters to $wpdb locally with $wpdb = new wpdb($user, $password, $db, $host); in a specific page I am not changing the database connection locally but everywhere (because of global)? If an other blog visitor clicks a link and causes an action on DB1 in time with setting $wpdb = $newdb there will be a conflict?
    – chabi
    Dec 27, 2013 at 13:44
  • @chabi you are not, global only applies to specific page load, it's not persistent between different page loads.
    – Rarst
    Dec 27, 2013 at 13:48
  • first of all thank you for your help. You got me, I am a bit confused. Could you provide some sources for further reading? Or just throw me some keywords for google :-)
    – chabi
    Dec 27, 2013 at 13:54
  • @chabi PHP manual is rather good :) See variable scope and rest of it.
    – Rarst
    Dec 27, 2013 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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