在多个自定义字段中的自定义帖子类型中对帖子进行排序acf

在多个自定义字段中的自定义帖子类型中对帖子进行排序acf

问题描述:

我在自定义帖子类型中有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"),有人可以帮助我。感谢

你需要查询的元田,使他们中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');