WordPress.org

Ready to get started?Download WordPress

Forums

Sort Query Posts
[resolved] Sorting Non-English Characters (8 posts)

  1. musicmann
    Member
    Posted 2 years ago #

    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: http://www.spanishconjugation.net.

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

    http://wordpress.org/extend/plugins/sort-query-posts/

  2. Tubal
    Member
    Plugin Author

    Posted 2 years ago #

    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.

    http://pastebin.com/gJtYqSjq

  3. musicmann
    Member
    Posted 2 years ago #

    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');
  4. Tubal
    Member
    Plugin Author

    Posted 2 years ago #

    If you're using UTF-8 encoding try:

    es_ES.utf8

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

    es_ES.iso88591

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

  5. musicmann
    Member
    Posted 2 years ago #

    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.

  6. Tubal
    Member
    Plugin Author

    Posted 2 years ago #

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

    http://pastebin.com/tPSK1r8p

  7. musicmann
    Member
    Posted 2 years ago #

    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!

  8. Tubal
    Member
    Plugin Author

    Posted 2 years ago #

    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 ;)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic