2017-02-07 44 views
2

我有一個自定義帖子類型cota,其中所有數據都顯示在我網站的<table>中。我需要通過以下方式訂購此表:WP_Query未正確排序

  • administradora - 某種用戶指定的「類別」。訂購ASC(a-z);
  • valor - 價格cota。每個cota都有價格。有序DESC(9 - 0);

問題:它已經通過administradora有序,但valor不正確排序。

例如:valor的訂單首先顯示一個cota,其中有9.000,00美元,然後另一個cota顯示爲1.000.000,00美元。這是不對的。這一百萬美元cota應該先到,然後是九千美元cota

Here's an image that shows better the situation

代碼訂購cota

 $query = new WP_Query(array(
     'post_type' => 'cota', 
     'posts_per_page' => -1, 
     'tax_query' => array(
      array(
       'taxonomy' => 'tipo', 
       'field' => 'slug', 
       'terms' => $atts['tipo'], 
     ), 
    ), 
     'orderby' => 'meta_value_num', 
     'meta_query' => array(
     'relation' => 'AND', 
     'valor_clause' => array(
      'key' => 'valor', 
      'compare' => 'EXISTS' 
     ), 
     'adm_clause' => array(
      'key' => 'administradora', 
      'compare' => 'EXISTS' 
     ) 
    ), 
     'orderby' => array(
     'adm_clause' => 'ASC', 
     'valor_clause' => 'DESC' 
    ) 
    ) 
); 

編輯1:經過一番研究,我發現WordPress的保存在數據庫中longtext它的所有數據。但仍然無法解決問題。

如果需要,我可以提供更多信息。

回答

0

嘗試下面的代碼:

$query = new WP_Query(array(
    'post_type' => 'cota', 
    'posts_per_page' => -1, 
    'tax_query' => array(
     array(
      'taxonomy' => 'tipo', 
      'field' => 'id', 
      'terms' => $atts['tipo'], 
    ), 
), 
    'orderby' => 'meta_value_num', 
    'meta_query' => array(
    'relation' => 'AND', 
    'valor_clause' => array(
     'key' => 'valor', 
     'compare' => 'EXISTS' 
    ), 
    'adm_clause' => array(
     'key' => 'administradora', 
     'compare' => 'EXISTS' 
    ) 
), 
    'orderby' => array(
    'adm_clause' => 'ASC', 
    'valor_clause' => 'DESC' 
) 
) 
); 
+0

沒有工作。如果我將'field'從'slug'改爲'id',則不會顯示數據。 –

0

解決

下面的代碼應該工作:

 $query = new WP_Query(array(
     'post_type' => 'cota', 
     'posts_per_page' => -1, 
     'tax_query' => array(
      array(
       'taxonomy' => 'tipo', 
       'field' => 'slug', 
       'terms' => $atts['tipo'], 
     ), 
    ), 
     'meta_query' => array(
     'relation' => 'AND', 
     'credito_clause' => array(
      'key' => "valor", 
      'orderby' => 'meta_value_num', 
      'type' => 'DECIMAL', 
      'compare' => 'EXISTS' 
     ), 
     'adm_clause' => array(
      'key' => 'administradora', 
      'compare' => 'EXISTS' 
     ) 
    ), 
     'orderby' => array(
     'adm_clause' => 'ASC', 
     'credito_clause' => 'DESC' 
    ) 
    ) 
);