Support » Plugin: Sort Query Posts » [Plugin: Sort Query Posts] Sorting Non-English Characters

  • Resolved musicmann


    Thanks for such a great plugin!

    I’m running into a slight issue where accented Spanish characters are not sorting correctly.

    For example, I have four titles that should sort like this (advertir, añadir, averiguar, avisar), but instead they sort like this (advertir, averiguar, avisar, añadir).

    Everything I’m reading suggests that Spanish should sort correctly without much issue. My character set is utf8. I’ve tried DB_COLLATE (in wp-config.php) as default (”), as utf8_general_ci, and utf8_spanish_ci. I also tried changing the “strnatcasecmp” to “strcasecmp” in sort-query-posts.php. All combinations are giving the same output.

    Also interesting is that I’m using Relevanssi as my search plugin, and it’s treating “n” and “ñ” as the same.

    If you want to take a look at the site, it’s located at:

    I’m more than willing to hack away at figuring this out, but any directional advice would be greatly appreciated.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Tubal


    You’re welcome musicmann!!

    Try this modified version of the plugin and report if it works.
    It allows you to set a locale for string comparison.

    Tubal, I couldn’t find a private message option, so I’ll reply here. I tried the changes. Here are my findings:

    • When I specify a correct Spanish locale, the ascending sort is (añ, ad, av). Different, but not what I expected. This is what the site is currently showing.
      Ex. sort_query_posts_by('title', 'asc','es_ES');
      Ex. sort_query_posts_by('title', 'asc','spanish');
    • When I specify English or an undefined locale, I get the original sorting. That is (ad, av, añ).
      Ex. sort_query_posts_by('title', 'asc','en_EN');
      Ex. sort_query_posts_by('title', 'asc','nonsense');
    • When I don’t specify a locale, I get the WordPress sorting.
      Ex. sort_query_posts_by('title', 'asc','');
      Ex. sort_query_posts_by('title', 'asc');
    Plugin Author Tubal


    If you’re using UTF-8 encoding try:


    or if you’re using ISO-8859-1 try:


    Also note that if your server is Windows, it will not work.

    I tried es_ES.utf8 (which matches my encoding according to wp-config.php), and it’s giving me the original sorting (ad, av, añ).

    I’m wondering if this is an issue with my host’s hardware/OS. I use BlueHost. I could try it out on another host that I have a hosting account for to see if I get something different.

    Plugin Author Tubal


    Give this version a try…you don’t need to change the function call.

    That worked!

    [Edit: Deleted to avoid confusion by anyone reading this in the future]

    I’m currently set on es_ES. I haven’t tried anything other than sorting titles. Let me know if you want me to do other test cases to make sure other sorts continue to occur as planned.

    I REALLY appreciate the help!

    Plugin Author Tubal


    I’m glad it did!!

    Note however, the correct way to compare multibyte strings requires to use the function strcoll:

    @setlocale(LC_ALL, $locale);
    $v = strcoll($s1, $s2);

    If that code does not work it doesn’t because of your host:
    – Your host may not allow you to change the locale in your scripts
    – Your host may not have the locale you need (es_ES) installed.

    The use of iconv to convert any string to ASCII before comparing is a hacky way of getting a similar but not perfect result.

    Rate the plugin if it proved useful to you 😉

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘[Plugin: Sort Query Posts] Sorting Non-English Characters’ is closed to new replies.