Fix wrong order in ACF gallery and relationship fields with WPML
We searched the ACF and WPML support forums but couldn’t find a suitable and reliable solution. After digging into the ACF source code we were able to fix the problem with a filter that we want to share with others having this problem.
To fix your order problems, copy/paste this code into your functions.php file:
function pixelbar_fix_wrong_acf_orders_with_ids( $value, $post_id, $field ) {
if ( !function_exists('icl_object_id') )
return $value;
$wpml_value = array();
foreach($value as $key=>$v) {
$id = icl_object_id($v);
if( is_int($id) ) {
$wpml_value[$key] = $id;
}
}
return $wpml_value;
}
add_filter('acf/format_value/type=relationship', 'pixelbar_fix_wrong_acf_orders_with_ids', 10, 3);
add_filter('acf/format_value/type=gallery', 'pixelbar_fix_wrong_acf_orders_with_ids', 10, 3);
This fix is for relationship and gallery fields and it doesn’t matter if they are in a repeater or flexible content field. This fix takes all the IDs in the array and searches the corresponding translation ID before giving the array to the get_posts() function.
It fixed our problem entirely.
If you want to expand this fix to other fields, just add another add_filter line with the field you want to expand.
add_filter('acf/format_value/type=THE_FIELD_TYPE', 'pixelbar_fix_wrong_acf_orders_with_ids', 10, 3);
Thanks guys! This did the job :D