How can I sort by meta value? In meta value I have “price” (float).
I make this query, but always get
Sorry, no posts matched your criteria.
$posts = query_posts( $query_string . '&orderby=meta_value_num&meta_key=Notes&meta_value_num=price&order=ASC');
Edit:
meta_value value:
"a:6:{s:5:"width";s:3:"580";s:6:"**price**";s:3:"99"".
Is it possible?
Your meta value is a serialized array.
What you’re asking it to get for you is all posts with a meta with the key
'Notes'
and the value'price'
. Your meta value is not'price'
, though, it’sThe first thing you need to do to order by price is to start using a helpful key/value structure. I assume you’re using something like this to store the post meta:
when your needs would indicate that you need to do something like this:
Once you have your meta values saved in that format, you’re going to want to use a
meta_query
:Meta queries are new in WordPress 3.1, so you’ll need to be using at least that version to do this.
Let me know if you run into problems!
Maybe this solution can help you, I needed to control posts sort by meta_key and meta_value on every request so I created a filter this way:
Ok, this solution is part of a big requirement but what we’re doing here is just intercepting every request, verify is there is a special sort option I added before and apply the corresponding sort by meta.
Hope this helps you.