You can’t order by values inside serialised arrays in post meta. When you store an array like this:
$value = [
[
'plan_name' => 'name 1',
'plan_price' => '1',
'theType' => 'yesPaid',
'plan_space' => 'Unlimited',
],
[
'plan_name' => 'name 2',
'plan_price' => '3',
'theType' => 'yesFree',
'plan_space' => '5000',
],
[
'plan_name' => 'name2',
'plan_price' => '5',
'theType' => 'yesPaid',
'plan_space' => 'Unlimited',
],
]
This is stored in PHP like this:
a:3:{i:0;a:4:{s:9:"plan_name";s:6:"name 1";s:10:"plan_price";s:1:"1";s:7:"theType";s:7:"yesPaid";s:10:"plan_space";s:9:"Unlimited";}i:1;a:4:{s:9:"plan_name";s:6:"name 2";s:10:"plan_price";s:1:"3";s:7:"theType";s:7:"yesFree";s:10:"plan_space";s:4:"5000";}i:2;a:4:{s:9:"plan_name";s:5:"name2";s:10:"plan_price";s:1:"5";s:7:"theType";s:7:"yesPaid";s:10:"plan_space";s:9:"Unlimited";}}
As far as MySQL — which is responsible for querying posts — is concerned, this is just a long string. Indistinguihsable from a word or sentence that begins with a
. It’s only turned back into an array by WordPress when the value is retrieved with get_post_meta()
.
So it’s not possible to sort by a property within an array, and if you attempt to sort by the meta value of this meta then it’s just going to sort as if this were a string.
If you need to sort this way then you need to find an alternate way of storing the data. I don’t have enough insight into your project to suggest anything specific, but you probably need a custom database table for these plans, which you’d then need to join and sort using the posts_join
and posts_orderby
hooks to modify the query SQL directly.