Skip to content

Instantly share code, notes, and snippets.

@xlplugins
Created April 23, 2026 05:31
Show Gist options
  • Select an option

  • Save xlplugins/9157f8f0e94c0dcbdf2d6d25cb86f5b6 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/9157f8f0e94c0dcbdf2d6d25cb86f5b6 to your computer and use it in GitHub Desktop.
Wpb flush
/**
* DB connection sync (shutdown)
* Description: Clears any pending MySQL result set on the default connection before other shutdown hooks run, reducing "Commands out of sync" errors from Action Scheduler, WooCommerce batch processing, Jetpack, and cron.
* Version: 1.0.0
*
* @package
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* When a previous query leaves results unconsumed, mysqli can throw "Commands out of sync" on the next query.
* wpdb::flush() frees the last result and drains multi-query state (see wp-includes/class-wpdb.php).
* Priority 0 runs before typical shutdown work (e.g. WooCommerce BatchProcessingController at default 10).
*/
add_action(
'shutdown',
static function () {
global $wpdb;
if ( $wpdb && method_exists( $wpdb, 'flush' ) ) {
$wpdb->flush();
}
},
0
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment