0

I'm having a problem with Wordpress migration, related to MySQL database.
I'm facing a charset encoding problem, so in the new hosting the letters with an accent, like "à", "è" are shown as a question mark.

I know from other projects that if, before a query I make this one

SET NAMES 'utf8'

the errors disappears.
I also tested it in a custom php page, trying to get a post content.

So, there is a way to tell wordpress to do this query before every post query?!

ilSavo
  • 854
  • 1
  • 8
  • 28

2 Answers2

1

Ok, I just solved by changing the database charset from latin_swedisc_ci to utf_8_general_ci and by editing the wp-config.php file, changing this row from
define('DB_CHARSET', 'utf8mb4');
to
define('DB_CHARSET', 'utf8');

ilSavo
  • 854
  • 1
  • 8
  • 28
  • See also http://stackoverflow.com/questions/38363566/trouble-with-utf8-characters-what-i-see-is-not-what-i-stored – Rick James Oct 21 '16 at 17:15
-1

You could try something like this in your functions.php file:

add_filter('posts_fields', 'my_posts_fields');

function my_posts_fields( $fields, &$wp_query ) {
    $fields = "SET NAMES 'utf8'; " . $fields;
}

This will adjust the SELECT clause of the main query so that instead of SELECT it becomes SET NAMES utf-8; SELECT

MirzaP
  • 738
  • 4
  • 11
  • Thanks MirzaP, I didn't try this solution because I've found in the meantime another solution, but it sound like your solution could have even worked – ilSavo Oct 20 '16 at 12:17
  • This is way overcomplicating things. Of course Wordpress can handle the database connection encoding correctly without needing to add query hacks in a filter. – deceze Oct 20 '16 at 12:44