Sorting Numeric Fields Using CAST

Sort Numbers (Currency and others) using CAST

When using SORT on a Numeric field, the numbers in Post Meta are stored as LONGTEXT so you need to cast them as numbers first.

CAST( product_page_weight.meta_value AS SIGNED ) DESC

Other Helpful Documentation on Sorting

Sorting DATE Fields Using CAST

When using SORT on a Date/Time field, the numbers in Post Meta are stored as LONGTEXT so you need to cast them as Date & Time Fields first. Typically, they are stored as yyyy-mm-dd hh:mm:ss but if you’re doing date/time math or sorting, you’ll solve a lot of issues by using CAST: CAST( date_time_field.meta_value AS …

Read more