query_posts: sorting by custom field numerically NOT alphanumerically
-
I need some guidance with sorting posts by custom field. I’ve read numerous similar posts here on www.remarpro.com, I’ve been on other sites… lets get to it…
I’m attempting to sort posts by a custom field using query_posts().
query_posts($query_string.'&meta_key=base_price&orderby=meta_value');
The custom field (base_price) is a numeric value that is a price. The sorting that happens appears to be alphanumeric with results sorted like:
850
7995
7350
650
etcI’ve already tried using orderby=meta_value_num and that produces the same results.
I need some sort of numerical sorting that takes into account the number of digits in the number (not just sorting by the first digit) so what is the best way to go about this?
it should be sorted like:
7995
7350
850
650
etcI’ve seen some similar posts where the post array was then resorted (after the query) and I’ve seen some suggestions about using a custom sql query (not using query_posts). Any input or suggestions from the community? How have you chosen to overcome this, if you’ve faced this before? It seems to me that the DEFAULT behavior of meta_value_num is not sufficient (if you then have to do more steps to get a better sort!).
- The topic ‘query_posts: sorting by custom field numerically NOT alphanumerically’ is closed to new replies.