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.

Hi I'm trying to pull posts using the wp query. Just one problem--I am trying to sort the posts alphabetically by a certain meta_value. Thing is that this meta_value is a serialized array. Is there an easy way to do this without having to unserialize it? Thanks!

a:1:{i:0;a:1:{s:7:"people";s:16:"Tanya Garca";}}

Btw, I cannot change the way the meta_value is being saved because I am using this plugin which automatically saves it serialized.

share|improve this question
    
similar question here. –  Milo Feb 17 '13 at 16:28
add comment

1 Answer

up vote 0 down vote accepted

Is there an easy way to do this without having to unserialize it?

Not in any reliable way. You can certainly ORDER BY that value but the sorting will use the whole serialized string, which will give you technically accurate results but not the results you want. You can't extract part of the string for sorting within the query itself. Even if you wrote raw SQL, which would give you access to database functions like SUBSTRING, I can't think of a dependable way to do it. You'd need a MySQL function that would unserialize the value-- you'd have to write it yourself.

Basically, if you need to sort on a meta_value you can't store it serialized. Sorry.

If you can't change how the value is stored you will need to pull your posts, loop through them to sort them, then loop through them again to display the sorted results.

share|improve this answer
    
Hi s_ha_dum! Thank you for your insights. I found a solution, which seems to work russellengland.com/2012/07/…. Do you think using substring_index will cause any problems in the future? –  KT. Feb 18 '13 at 4:30
    
I only glanced at that but it looks like it should work if your data is always in the same order in the serialized string. If some other value ever gets inserted into the string before the data you want that won't work. The author of that solution even states as much: "It does depend on the developer knowing how many columns are in the serialized data and the structure being the same." So, yes, I would have to say it could definitely cause problems in the future. –  s_ha_dum Feb 18 '13 at 4:43
    
In my case, I am sure about the structure of the array being created so I guess this should work okay. Thanks for looking into it. –  KT. Feb 18 '13 at 5:10
    
It is interesting that you "cannot change the way the meta_value is being saved because [you are] using this plugin" but you can be sure about the structure of the data and that the structure won't change. That is a lot of the reason I said "no reliable way". If that plugin changes so might the array. Just be aware. –  s_ha_dum Feb 18 '13 at 5:17
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.