WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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.

share|improve this answer
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 );? – Adam Capriola Jul 29 '12 at 16:16

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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