Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TeemuSuoranta/2174f78f37248aeef483526d1c5d176f to your computer and use it in GitHub Desktop.
Save TeemuSuoranta/2174f78f37248aeef483526d1c5d176f to your computer and use it in GitHub Desktop.
/**
* Add support for correct UTF8 orderby for post_title (äöå)
*
* @param string $orderby ordering clause for query
*
* @return string ordering clause for query
*/
add_filter('posts_orderby', function($orderby) use ($wpdb) {
if(strstr($orderby, 'post_title')) {
$order = (strstr($orderby, 'post_title ASC') ? 'ASC' : 'DESC');
$old_orderby = $wpdb->posts . '.post_title ' . $order;
$utf8_orderby = 'CONVERT ( LCASE(' . $wpdb->posts . '.post_title) USING utf8) COLLATE utf8_bin ' . $order;
// replace orderby clause in $orderby
$orderby = str_replace($old_orderby, $utf8_orderby, $orderby);
}
return $orderby;
});
/**
* Add support for correct UTF8 orderby for term name (äöå)
*
* @param string $orderby ordering clause for terms query
* @param array $this_query_vars an array of terms query arguments
* @param array $this_query_vars_taxonomy an array of taxonomies
*
* @return string ordering clause for terms query
*/
add_filter('get_terms_orderby', function($orderby, $this_query_vars, $this_query_vars_taxonomy) {
if(strstr($orderby, 't.name')) {
$old_orderby = 't.name';
$utf8_orderby = 'CONVERT ( LCASE(t.name) USING utf8) COLLATE utf8_bin ';
// replace orderby clause in $orderby
$orderby = str_replace($old_orderby, $utf8_orderby, $orderby);
}
return $orderby;
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment