WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

I have a list of products on a page and am trying to order them by a custom field called "price". Unfortunately, $3,950,000 is coming before $349,000. I've used a couple of codes but neither one is working. Does anyone have any suggestions? Thanks.

Here's the first code I've tried.

$query = new WP_Query( array ( 'post_type' => 'product', 'orderby' => 'meta_value', 'meta_key' => 'price', 'order' => 'ASC' ) );

Here's the other code I've tried.

$query = new WP_Query( array ( 'post_type' => 'product', 'orderby' => 'meta_value_num+0', 'meta_key' => 'price', 'order' => 'ASC' ) );
share|improve this question
up vote 1 down vote accepted

You are storing your "price" (apparently) as human readable formatted currency. That makes it a string. And that means it is going to be sorted alphabetically, more or less, and not numerically as you need it to be. If you can store those values without the punctuation, and use meta_value_num-- your second query above (but I am not sure you need the "+0") -- I think you will have what you want.

share|improve this answer
That works! Thank you. Now I just need to find some code that will put the commas back in after the query. – Michael Dec 8 '12 at 0:35
PHP's number_format should get you a long way. – s_ha_dum Dec 8 '12 at 1:09
Make sure you use WordPress's i18n version of number format, so you get the internationalization benefits. – Otto Dec 9 '12 at 0:19

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.