2016-04-18 157 views
0

我有一個名爲「sale_status」的自定義字段,值(「出售」,「出售」,「讓」)顯示在管理單選按鈕中。現在,每個人都可以分配到一個屬性。通過wordpress中的多個自定義字段值排序

目前查詢通過發佈日期提取屬性順序,但我希望它是由sale_status然後日期。

我的代碼below--

$args = array(
     'post_type'  => 'zoo-property', 
     'posts_per_page' => $query__per_page, 
     'post_status'  => 'publish', 
     'paged'   => $query__page,    
     'meta_key'  => 'sale_status', 
     'orderby'   => 'meta_value_num', 
     'order'   => 'DESC', 

     'meta_query' => array(
     'relation' => 'AND', 
     $query__types, 
     $query__locations, 
     $query__statuses, 
     $query__investments, 
     $query__price 
    ) 

    ); 


    $properties_wp_query = new WP_Query($args); 
    echo "Last SQL-Query: {$properties_wp_query->request}"; 

但在正確的順序也沒有顯示。

任何幫助,高度讚賞。提前致謝。

回答

2

如果要在同一個查詢中包含多個排序條件,則orderby值必須是數組。詳情請參閱WP_Query documentation。試試這樣的:

$args = array(
    'post_type'  => 'zoo-property', 
    'posts_per_page' => $query__per_page, 
    'post_status'  => 'publish', 
    'paged'   => $query__page,    
    'meta_key'  => 'sale_status', 
    'orderby'   => array('meta_value_num' => 'DESC', 'post_date' => 'DESC'), 
    'meta_query' => array(
     'relation' => 'AND', 
     $query__types, 
     $query__locations, 
     $query__statuses, 
     $query__investments, 
     $query__price 
    ) 
); 
+0

你也可以使用meta_value_num meta_value_num。只有當您想爲每個有序字段設置不同的排序順序時,才需要該數組(該功能[已添加到WP 4.0中](https://make.wordpress.org/core/2014/08/29/a-more -powerful階由合的wordpress-4-0 /))。 – vard

+0

感謝您指出這一點! – Technoh

+0

@Technoh非常感謝您的幫助。它像一個魅力。只是想知道是否有可能首先顯示全部「待售」,然後是全部「已售出」,然後全部「讓」 – Raj

相關問題