2015-08-28 76 views
1

我在自定義帖子類型中有4個文本字段類型,這些是brand_fbn,model_fbn,year_fbnplate_number。我想對文章進行排序,編號爲brand_fbn - model_fbn - year_fbn - plate_number。但是我堅持這一說法,在多個自定義字段中的自定義帖子類型中對帖子進行排序acf

這是我的代碼(錯誤)

     $ins = new WP_Query(array(
          "post_type" =>  "application-list", 
          "author" =>  $current_user->ID, 
          "meta_key"  => array("year_fbn","plate_number"), 
          "orderby"  => "meta_value ", 
          "order"   => "ASC" 
         )); 

我知道這條線不會工作,"meta_key"=>array("year_fbn","plate_number"),有人可以幫助我。感謝

回答

0

你需要查詢的元田,使他們中WP_QueryJOIN版,然後使用posts_orderby過濾那些元字段訂購您的結果 - 別名使用將在格式mt1.meta_value

// Function to replace the order by clause in the SQL generated by WP_Query 
function custom_order_by($order_by){ 
    return str_replace( 
     'menu_order', 
     'mt1.meta_value, mt2.meta_value, mt3.meta_value, mt4.meta_value', 
     $order_by 
    ); 
} 

// args for WP_Query 
$args = array(
    "post_type" => "application-list", 
    "author"  => $current_user->ID, 
    "meta_query" => array(
     array( 
      "key"  => "brand_fbn", 
      "compare" => "LIKE", 
      "value" => "" 
     ), 
     array( 
      "key"  => "model_fbn", 
      "compare" => "LIKE", 
      "value" => "" 
     ), 
     array( 
      "key"  => "year_fbn", 
      "compare" => "LIKE", 
      "value" => "" 
     ), 
     array( 
      "key"  => "plate_number", 
      "compare" => "LIKE", 
      "value" => "" 
     ) 
    ) 
); 

// add filter to modify order by clause 
add_filter('posts_orderby', 'custom_order_by'); 
// run the WP_Query 
$ins = new WP_Query($args); 
// remove the filter so that other queries aren't affected. 
remove_filter('posts_orderby', 'custom_order_by'); 
相關問題