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)?