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.
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.
As stated above, your orderby
parameter should be set to meta_value
, not restaurant
.
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.
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
$args
argument used in aWP_Query
, orpre_get_posts
, or (shudder)query_posts()
, for example). – Pat J Commented Nov 20, 2018 at 17:38