Last active
August 27, 2019 10:42
-
-
Save TeemuSuoranta/2174f78f37248aeef483526d1c5d176f to your computer and use it in GitHub Desktop.
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
/** | |
* 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