0

I'm trying to sort custom posts in alphabetical order, and I've just now realized that capital letters are being sorted before lowercase. Two restaurants start with 'Cal' and 'CAT' with 'CAT' being returned as the first in alphabetical order.

Here're my $args:

$args = array(
    'numberposts' => -1,
    'post_type' => 'chef',
    'meta_key' => 'restaurant',
    'orderby' => 'meta_value',
    'order' => 'ASC'
);

I've also tried changing the 'orderby' to 'LOWER(restaurant)' but had no success with that.

  • Please edit your question to provide more context (is your $args argument used in a WP_Query, or pre_get_posts, or (shudder) query_posts(), for example). – Pat J Nov 20 '18 at 17:38
0
  1. As stated above, your orderby parameter should be set to meta_value, not restaurant.

  2. Using phpMyAdmin or some other tool, look at your database. With any newer installation of WordPress, your tables should have a collation of utf8mb4_unicode_ci. That means that the table is storing its data in UTF-8 4-byte unicode character set. That "ci" at the end means "case insensitive".

If your collation is not correct, then you may need to convert your data. This is not as easy as it sounds all the time, so make sure you get a quality backup of your database first.

  • Interesting. Thanks for that tidbit. I'll have a look. It's on Pantheon, so I'm not sure I can change it. – KingRichard Nov 19 '18 at 22:53
  • Yeah, all collations on wp_postmeta:meta_value are utf8mb4_unicode_ci so this is not the problem. – KingRichard Nov 19 '18 at 23:17
0

It looks like you may have set your orderby incorrectly... you don't set the metakey and orderby to the same.

try changing

'orderby' => 'restaurant',

to

'orderby' => 'meta_value',

this link may help https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

  • Per the docs you posted, it should be orderby => 'meta_value' to sort by the restaurant metadata. – Pat J Nov 19 '18 at 22:27
  • 1
    ack You're correct. – rudtek Nov 19 '18 at 22:49
  • Thanks for that, but it doesn't change my sort at all. 'CAT' still shows up before 'Cal' – KingRichard Nov 19 '18 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.