我正在测试一些东西,我有这样一个问题:
$paged = 1;
$filter_min_price = 5;
$filter_max_price = 300;
$products5_cat_term_id = 368;
$filter_brand = "Now, Garmin";
$sort_order = "ASC";
$the_query = array(
\'post_type\' => \'products5\', // name of post type.
\'tax_query\' => array(
array(
\'taxonomy\' => \'products5_categories\', // taxonomy name
\'field\' => \'term_id\', // term_id, slug or name
\'terms\' => $products5_cat_term_id,
)
),
\'posts_per_page\' => 100,
\'paged\' => $paged,
\'post_status\' => \'publish\',
\'meta_query\' => array(
\'relation\' => \'AND\',
\'price_clause\' => array(
\'key\' => \'kixx_product_price\',
\'value\' => array( $filter_min_price, $filter_max_price ),
\'type\' => \'DECIMAL\',
\'compare\' => \'BETWEEN\',
),
array(
\'relation\' => \'OR\',
\'brand_clause\' => array(
\'key\' => \'kixx_product_brand\',
\'value\' => $filter_brand,
\'compare\' => \'IN\',
),
),
),
\'orderby\' => array(
\'price_clause\' => $sort_order,
\'brand_clause\' => $sort_order,
),
);
$t5_products_query = new WP_Query($the_query);
if ($t5_products_query->have_posts()) {
while($t5_products_query->have_posts()) : $t5_products_query->the_post();
$current_post_id = get_the_ID();
$kixx_product_price = get_post_meta($current_post_id, "kixx_product_price", true);
$kixx_product_brand = get_post_meta($current_post_id, "kixx_product_brand", true);
$kixx_merchant_id = get_post_meta($current_post_id, "kixx_merchant_id", true);
echo "$kixx_product_price is: $current_post_id and kixx_product_brand: $kixx_product_brand and kixx_merchant_id: $kixx_merchant_id<br>";
endwhile;
} else {
// No products matched criteria
}
我希望得到的是按
price 作为第一个条件,然后
product_brand (ASC或DESC)。。。
为什么wordpress不能像我明确放置的那样对小数进行正确排序kixx_product_price
作为十进制值,但它仍然不会?
以下是输出:
9.94 is: 8763 and kixx_product_brand: Now and kixx_merchant_id: 100452976
15.41 is: 5383 and kixx_product_brand: Garmin and kixx_merchant_id: 100452976
15.41 is: 5569 and kixx_product_brand: Garmin and kixx_merchant_id: 100452976
15.16 is: 3485 and kixx_product_brand: Now and kixx_merchant_id: 100452976
15.06 is: 3629 and kixx_product_brand: Now and kixx_merchant_id: 100452976
15.17 is: 4785 and kixx_product_brand: Now and kixx_merchant_id: 100452976
15.17 is: 4865 and kixx_product_brand: Now and kixx_merchant_id: 100452976
15.11 is: 5149 and kixx_product_brand: Now and kixx_merchant_id: 100452976
15.17 is: 5857 and kixx_product_brand: Now and kixx_merchant_id: 100452976
15.17 is: 6973 and kixx_product_brand: Now and kixx_merchant_id: 100452976
15.17 is: 7750 and kixx_product_brand: Now and kixx_merchant_id: 100452976
16.36 is: 5269 and kixx_product_brand: Garmin and kixx_merchant_id: 100452976
16.30 is: 4367 and kixx_product_brand: Now and kixx_merchant_id: 100452976
非常感谢您的帮助!
编辑:我看到了这个查询,它是这样的:
SELECT SQL_CALC_FOUND_ROWS
{$wpdb->prefix}posts.ID FROM {$wpdb->prefix}posts
LEFT JOIN {$wpdb->prefix}term_relationships ON ({$wpdb->prefix}posts.ID = {$wpdb->prefix}term_relationships.object_id)
INNER JOIN {$wpdb->prefix}postmeta ON ( {$wpdb->prefix}posts.ID = {$wpdb->prefix}postmeta.post_id )
INNER JOIN {$wpdb->prefix}postmeta AS mt1 ON ( {$wpdb->prefix}posts.ID = mt1.post_id )
WHERE 1=1
AND ( {$wpdb->prefix}term_relationships.term_taxonomy_id IN (368))
AND ( ( {$wpdb->prefix}postmeta.meta_key = \'kelkoo_product_price\' AND CAST({$wpdb->prefix}postmeta.meta_value AS DECIMAL(10,2)) BETWEEN \'5\' AND \'300\' )
AND (( mt1.meta_key = \'kelkoo_product_brand\' AND mt1.meta_value IN (\'Now\',\'Garmin\') )))
AND {$wpdb->prefix}posts.post_type = \'products5\'
AND (({$wpdb->prefix}posts.post_status = \'publish\'))
GROUP BY {$wpdb->prefix}posts.ID
ORDER BY CAST({$wpdb->prefix}postmeta.meta_value AS DECIMAL) ASC,
CAST(mt1.meta_value AS CHAR) ASC
LIMIT 0, 100
所以很明显,问题是从底部算起的第三行,从哪里算起
DECIMAL 而不是
DECIMAL(10,2)... 当我手动运行查询时,我得到了预期的结果,但不确定如何修改WP查询以添加
DECIMAL(10,2) ?