Let’s say I’m modifying the default posts query:
add_filter( 'pre_get_posts', 'my_filter' );
function my_filter( $query ) {
$query->set( 'meta_key', 'my_rating' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DESC' );
return $query;
}
I know this works for the meta_key my_rating
if its value is a number. But let’s say its value is an array whose values are numbers. Like this for example:
array(
"foo" => 1,
"bar" => 2,
)
But the array would be serialized and I guess need to be unserialized to use it. Is there any way to pick which key in the array to sort by?
Your array is a PHP construct and only meaningful to PHP, as far as MySQL is concerned it’s just a string of characters. If you need to query on that data it should be separated out into individual keys.