对自定义元进行自定义WP查询并按多个元键值排序

时间:2019-06-04 作者:Peter

我正在测试一些东西,我有这样一个问题:

$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) ?

1 个回复
最合适的回答,由SO网友:nmr 整理而成

您可以使用\'type\' => \'DECIMAL(10, 2)\', 在元查询中。

文件中提到了这一点Custom_Field_Parameters.

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post