order by meta_value serialized array

Is there an easy way to do this without having to unserialize it?

Not in any reliable way. You can certainly ORDER BY that value but the sorting will use the whole serialized string, which will give you technically accurate results but not the results you want. You can’t extract part of the string for sorting within the query itself. Even if you wrote raw SQL, which would give you access to database functions like SUBSTRING, I can’t think of a dependable way to do it. You’d need a MySQL function that would unserialize the value– you’d have to write it yourself.

Basically, if you need to sort on a meta_value you can’t store it serialized. Sorry.

If you can’t change how the value is stored you will need to pull your posts, loop through them to sort them, then loop through them again to display the sorted results.

Leave a Comment