2016-08-26 60 views
0

這是WP_Query我目前的參數:Complex WordPress Orderby參數:如何使用meta_value_num查詢多個orderby參數?

array(5) { 
    ["posts_per_page"]=> 
    int(6) 
    ["orderby"]=> 
    array(2) { 
    ["uss_product_price"]=> 
    string(3) "ASC" 
    ["uss_product_weight"]=> 
    string(4) "DESC" 
    } 
    ["meta_query"]=> 
    array(3) { 
    ["relation"]=> 
    string(3) "AND" 
    ["sortprimary_clause"]=> 
    array(2) { 
     ["key"]=> 
     string(17) "uss_product_price" 
     ["compare"]=> 
     string(6) "EXISTS" 
    } 
    ["sortsecondary_clause"]=> 
    array(2) { 
     ["key"]=> 
     string(18) "uss_product_weight" 
     ["compare"]=> 
     string(6) "EXISTS" 
    } 
    } 
    ["paged"]=> 
    string(1) "1" 
    ["post_type"]=> 
    string(7) "product" 
} 

這種新的排序依據的是在WordPress 4.0和4.2介紹: https://make.wordpress.org/core/2014/08/29/a-more-powerful-order-by-in-wordpress-4-0/ https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

查詢基本上是由字符串工作但產品訂購uss_product_price和uss_product_weight的值。以前,這一問題終於解決了在WP_Query給meta_value_num而不是meta_value: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

我想要的結果通過uss_product_price的數值(第一次)進行排序和uss_product_weight(秒)。如何在使用多個orderby參數的複雜度的同時告訴查詢使用數值?

回答

0

答案很簡單 - 在元查詢中添加'type'=>'numeric'。

array(5) { 
    ["posts_per_page"]=> 
    int(6) 
    ["orderby"]=> 
    array(2) { 
    ["uss_product_price"]=> 
    string(4) "DESC" 
    ["uss_product_weight"]=> 
    string(4) "DESC" 
    } 
    ["meta_query"]=> 
    array(3) { 
    ["relation"]=> 
    string(3) "AND" 
    ["sortprimary_clause"]=> 
    array(3) { 
     ["key"]=> 
     string(17) "uss_product_price" 
     ["compare"]=> 
     string(6) "EXISTS" 
     ["type"]=> 
     string(7) "numeric" 
    } 
    ["sortsecondary_clause"]=> 
    array(2) { 
     ["key"]=> 
     string(18) "uss_product_weight" 
     ["compare"]=> 
     string(6) "EXISTS" 
    } 
    } 
    ["paged"]=> 
    string(1) "1" 
    ["post_type"]=> 
    string(7) "product" 
}