最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Order posts by price

programmeradmin3浏览0评论

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' ) );

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 asked Dec 7, 2012 at 23:56 MichaelMichael 131 silver badge3 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

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.

Good Reference from below link:

https://therichpost/wordpress-query-to-get-woocommerce-products-order-by-sales-pricing-and-rating/

'orderby' => 'meta_value_num',
'order' => 'asc',
'meta_key' => '_price'

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论