首先从args中删除order部分,并添加嵌套的meta\\u查询,如以下示例所示:
$args_com_subset = array(
\'posts_per_page\' => -1,
\'post_type\' => \'company\',
\'meta_key\' => \'company_tiers\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'company_tiers\',
\'value\' => array( 1,2,3,4 ),
\'compare\' => \'IN\',
),
array(
\'relation\' => \'OR\',
array(
\'key\' => \'company_type\',
\'value\' => \'full_methodology_n_subset_of_indicators\',
\'compare\' => \'=\'
),
array(
\'key\' => \'company_type\',
\'value\' => \'subset_of_indicators\',
\'compare\' => \'=\'
)
)
),
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'company_year\',
\'field\' => \'slug\',
\'terms\' => \'2020\'
)
)
);
然后,您必须添加一个过滤器来更改查询的排序部分,请记住在执行查询后删除此过滤器,以确保它不会影响任何其他查询:
add_filter( \'posts_orderby\', \'meta_key\' );
$posts = new WP_Query($args);
remove_filter( \'posts_orderby\', \'custom_sql_order\' );
function custom_sql_order($orderby) {
global $wpdb;
$orderby = "FIELDS(\'wp_postmeta.meta_value\', 3,2,1,4)";
return $orderby;
}