我有这些数据:
id meta_key meta_value
1 d_t_w 4 weeks
2 d_t_w 2 Days
3 d_t_w 3 Weeks
4 d_t_w 4 Days
5 d_t_w 1 Week
WP查询:
$query['orderby'] = 'd_t_w';
$query['order'] = ASC;
$query = new WP_Query($query);
结果:
id meta_key meta_value
5 d_t_w 1 Week
2 d_t_w 2 Days
3 d_t_w 3 Weeks
4 d_t_w 4 Days
1 d_t_w 4 weeks
但我的期望是:
id meta_key meta_value
2 d_t_w 2 Days
4 d_t_w 4 Days
5 d_t_w 1 Week
3 d_t_w 3 Weeks
1 d_t_w 4 weeks
如何按照自定义价值(1天,2天,3天,......,3周,4周)申请订单
答案 0 :(得分:1)
如果您的表meta_value
后缀与周数相同,那么您可以使用此方法
SELECT meta_value,
REPLACE(
REPLACE(
REPLACE(REPLACE(meta_value,' weeks','00'),' week','00'),' Days','0'),' Day','0'
) as odr
FROM `product`
ORDER by CAST(odr AS UNSIGNED)
答案 1 :(得分:0)
在原始查询语言(wordpress indipendent)中,您可以提取字段并计算字符串的天数以订购查询
像这样SELECT
*
FROM (
SELECT '1 Week' as c
UNION ALL SELECT '2 Days'
UNION ALL SELECT '3 Days'
UNION ALL SELECT '3 Weeks'
UNION ALL SELECT '1 Day'
) as t
ORDER BY TRIM(SUBSTR(t.c, 1, LOCATE(' ', t.c))) *
IF(TRIM(SUBSTR(LOWER(t.c), LOCATE(' ', t.c))) = 'week', 7,
IF(TRIM(SUBSTR(LOWER(t.c),LOCATE(' ', t.c))) = 'weeks', 7,
IF(TRIM(SUBSTR(LOWER(t.c), LOCATE(' ', t.c))) = 'day', 1,
IF(TRIM(SUBSTR(LOWER(t.c), LOCATE(' ', t.c))) = 'days', 1,
1)))) ASC;
答案 2 :(得分:0)
您的查询应该是这样的:
$args = array(
'orderby' => 'meta_value',
'meta_key' => 'meta_value',
'order' => 'ASC',
);
$query = new WP_Query( $args );
答案 3 :(得分:0)
您可以使用正常的日期/时间order_by ASC / DESC进行查询,然后将元值转换为d_t_w。
答案 4 :(得分:0)
我通过添加过滤器解决了这个问题:
add_filter( 'posts_orderby', 'deliver_to_weeks', 11, 2 );
function deliver_to_weeks( $orderBy, $query ) {
$orderBy = str_ireplace('wp_postmeta.meta_value', "field(wp_postmeta.meta_value, '1 Day', '2 Days', '3 Days', '4 Days', '5 Days' , '1 Week', '2 Weeks', '3 Weeks', '4 Weeks')", $orderBy);
return $orderBy;
}