admin管理员组文章数量:1391968
I've created a custom post field for use on my site that is the price of a real estate item for sale (that is the subject of the post). It should sort posts archives by price but I cannot get it to sort high to low; it still sorts as follows:
$2,900,000
$24,450,000
$1,900,000
$13,000,000
The client will be adding posts so it needs to be easy for them to enter the value as dollars.
I've tried casting the input as UNSIGNED and also as NUMERIC and have tried other things such as meta_key_num instead of meta_key.
Here is what I have currently, which "almost" works.
add_action( 'pre_get_posts', 'kr_change_posts_order' );
// start - from Toolset
function func_orderby_asking_price( $orderby ) {
global $WP_Views;
if($WP_Views) {
$orderby = str_replace( 'wp_postmeta.meta_value', "cast(replace(trim( leading '$' from wp_postmeta.meta_value),',','') AS UNSIGNED)", $orderby ); // also tried NUMERIC
}
return $orderby;
}
add_filter('posts_orderby', 'func_orderby_asking_price' );
// end from Toolset
function kr_change_posts_order( $query ) {
if ( $query->is_main_query() && !is_admin() ) {
$orderby = genesis_get_custom_field( 'price' );
$query->set( 'cat', ( array( 5, 6, 15 ) ) );
$query->set( 'meta_key', 'price' );
$query->set( 'orderby', 'meta_value' ); // also tried meta_value_num
$query->set( 'order', 'DESC' );
}
}
These are actually two pieces of code (functions) that I found and modified. I've also tried keeping the functions separated instead of one within the other, but so far have not found a solution to get the sorting to work correctly.
I have looked for a solution on this site and elsewhere for a couple of days but no luck.
Thanks in advance for your help.
KJR
I've created a custom post field for use on my site that is the price of a real estate item for sale (that is the subject of the post). It should sort posts archives by price but I cannot get it to sort high to low; it still sorts as follows:
$2,900,000
$24,450,000
$1,900,000
$13,000,000
The client will be adding posts so it needs to be easy for them to enter the value as dollars.
I've tried casting the input as UNSIGNED and also as NUMERIC and have tried other things such as meta_key_num instead of meta_key.
Here is what I have currently, which "almost" works.
add_action( 'pre_get_posts', 'kr_change_posts_order' );
// start - from Toolset
function func_orderby_asking_price( $orderby ) {
global $WP_Views;
if($WP_Views) {
$orderby = str_replace( 'wp_postmeta.meta_value', "cast(replace(trim( leading '$' from wp_postmeta.meta_value),',','') AS UNSIGNED)", $orderby ); // also tried NUMERIC
}
return $orderby;
}
add_filter('posts_orderby', 'func_orderby_asking_price' );
// end from Toolset
function kr_change_posts_order( $query ) {
if ( $query->is_main_query() && !is_admin() ) {
$orderby = genesis_get_custom_field( 'price' );
$query->set( 'cat', ( array( 5, 6, 15 ) ) );
$query->set( 'meta_key', 'price' );
$query->set( 'orderby', 'meta_value' ); // also tried meta_value_num
$query->set( 'order', 'DESC' );
}
}
These are actually two pieces of code (functions) that I found and modified. I've also tried keeping the functions separated instead of one within the other, but so far have not found a solution to get the sorting to work correctly.
I have looked for a solution on this site and elsewhere for a couple of days but no luck.
Thanks in advance for your help.
KJR
Share Improve this question asked Feb 27, 2020 at 16:36 KJRwebdevKJRwebdev 12 bronze badges 4 |1 Answer
Reset to default 0Thanks to Brad Dalton at wpsites for getting me to the answer.
Install Advanced Custom Fields plugin. Create a custom field called price, formatted as a number.
Use the code below (based on Bill Erickson's tutorial):
add_action( 'pre_get_posts', 'kr_change_posts_order' );
function kr_change_posts_order( $query ) {
if ( $query->is_main_query() && !is_admin() && !is_page() ) {
$orderby = genesis_get_custom_field( 'price' );
$query->set( 'cat', ( array( 5, 6, 15 ) ) );
$query->set( 'meta_key', 'price' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', 'DESC' );
}
}
This sorts by price, descending, for three post categories with IDs 5, 6 and 15.
本文标签: postsWPquery sort by custom metakey quotpricequot
版权声明:本文标题:posts - WP_query sort by custom meta_key "price" 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744703851a2620722.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$
and,
. You will have a considerably easier time if you save them as numbers, and format them only on output. – Jacob Peattie Commented Feb 27, 2020 at 23:28