Last active
October 24, 2019 18:59
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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