Skip to content

Instantly share code, notes, and snippets.

@matpratta
Last active October 24, 2019 18:59
Show Gist options
  • Save matpratta/4f3acb29ac4fb0e9a03318bcacec1972 to your computer and use it in GitHub Desktop.
Save matpratta/4f3acb29ac4fb0e9a03318bcacec1972 to your computer and use it in GitHub Desktop.
πŸ”§ 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