使用 woocommerce 在自定义排序中添加 meta_type [英] Add meta_type in custom sorting using woocommerce
问题描述
我正在使用以下代码在 woocommerce 中进行自定义排序.这工作正常.唯一的问题是我必须根据数字进行排序.我使用了 meta_type = UNSIGNED
但这不起作用.
I am using below code for custom sorting in woocommerce. This is working fine. The only thing is that I have to sort according to numbers. I have used meta_type = UNSIGNED
but this is not working.
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );
function custom_woocommerce_get_catalog_ordering_args( $args ) {
$orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
if ( 'timer' == $orderby_value ) {
$args['meta_key'] = '_auction_dates_to_time';
$args['meta_type'] = 'UNSIGNED';
$args['orderby'] = "meta_value";
$args['order'] = 'ASC';
$args['paged'] = $paged;
}
if ( 'timer-desc' == $orderby_value ) {
$args['meta_key'] = '_auction_dates_to_time';
$args['meta_type'] = 'UNSIGNED';
$args['orderby'] = "meta_value";
$args['order'] = 'DESC';
$args['paged'] = $paged;
}
return $args;
}
add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['timer'] = 'Sort by Bid End Time: low to high';
$sortby['timer-desc'] = 'Sort by Bid End Time: high to low';
return $sortby;
}
请帮助我解决这个问题.
Please HELP me on this issue.
谢谢
推荐答案
再次阅读文档和源代码.无论您在查询中将 meta_type
设置为什么(实际上被忽略了,文档中没有这样的参数),实际数据仍然存储在 wp_postmeta
表中,<具有 LONGTEXT
类型的 code>meta_value 列.对 LONGTEXT
列进行排序应该会给你这些结果.这就是字典顺序的工作方式.
Read the docs and the sources again.
Regardless of what you set meta_type
to in the query (it's actually ignored, there's no such parameter in the docs), the actual data is still stored in the wp_postmeta
table, meta_value
column that has a LONGTEXT
type.
Sorting on a LONGTEXT
column is supposed to give you those results. That's the way lexicographic order works.
唯一你能做的就是将该列转换为数字类型,从而告诉数据库它应该进行数字排序.您可以使用自定义 SQL 查询来执行此操作,或使用this 内置 WP_Query
功能:
The only thing you can do is CAST that column to a numeric type thus telling the database it should do a numeric sort.
You do that with a custom SQL query, or use this built-in WP_Query
feature:
orderby (string | array) - 按参数对检索到的帖子进行排序
orderby (string | array) - Sort retrieved posts by parameter
'meta_value_num' - 按数字元值排序(可用于2.8 版).另请注意,还必须存在meta_key=keyname"在查询中.该值允许进行如上所述的数字排序在元值"中.
'meta_value_num' - Order by numeric meta value (available with Version 2.8). Also note that a 'meta_key=keyname' must also be present in the query. This value allows for numerical sorting as noted above in 'meta_value'.
幸运的是,woocommerce 通过设置:$args['orderby'] = "meta_value_num";
因为 $args
直接发送到 WP_Query代码>.
Luckily woocommerce exposes exactly that by setting: $args['orderby'] = "meta_value_num";
because $args
is directly sent to WP_Query
.
这个告诉WP_Query
将 cast meta_value
转换为数字类型(使用 +0
):
This tells WP_Query
to cast meta_value
to a numeric type (using +0
):
case 'meta_value_num':
$orderby = "$wpdb->postmeta.meta_value+0";
break;
这篇关于使用 woocommerce 在自定义排序中添加 meta_type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!