Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
🔧 Fixes errors regarding ONLY_FULL_GROUP_BY when using WordPress with managed database services using MySQL 8.
<?php
/*
Plugin Name: WP Managed MySQL 8 Fix
Plugin URI: https://gist.github.com/MatheusMK3/4f3acb29ac4fb0e9a03318bcacec1972
Description: Fixes issues regarding MySQL modes (ONLY_FULL_GROUP_BY) when on managed database services.
Author: Matheus Pratta
Version: 1.0.0
Author URI: https://matheus.io
*/
add_action('init', 'wp_fixes_mysql8_full_group_by', -1);
function wp_fixes_mysql8_full_group_by () {
global $wpdb;
// Parse all our current modes
$currentModes = explode(',', array_pop($wpdb->get_results('SELECT @@sql_mode'))->{'@@sql_mode'});
// Find the offending index (ONLY_FULL_GROUP_BY)
$removeIndex = array_search('ONLY_FULL_GROUP_BY', $currentModes);
// If found, unset it
if (false !== $removeIndex) unset($currentModes[$removeIndex]);
// Sets the SQL mode
$wpdb->set_sql_mode($currentModes);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.