Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.