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:

    "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?

1 Answer 1


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.

  • Thanks for the info. How would I go about separating the data into individual keys? Something like $data = get_post_meta( $post->ID, 'my_rating', true); $number = $data['foo']; And then $query->set( 'meta_key', $number );? Jul 29, 2012 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.