Frisch notiert!
Die neusten Artikel aus unserem Blog.

Fix wrong order in ACF gallery and relationship fields with WPML

von Adrian Lambertz / Zuletzt aktualisiert vor 1 Monat / Allgemein / Server & Technik / Support / Wordpress / 0
Recently we had some issues with websites using ACF and WPML in combination. Our gallery and relationship fields were completely messed up in their order after duplicating the content into a new language. The fields from the default language were fine. Just the translation was messed up.

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);

Adrian Lambertz

Seit 2010 bin ich nun schon bei Pixelbar mit dabei. Zuerst als Auszubildender und nach erfolgreichem Abschluss meiner Ausbildung als Frontend-Entwickler.

Ohne Musik und Kaffee kann ich nicht leben, daher konsumiere ich beides während der Arbeit praktisch durchgehend :).

Daneben liebe ich WordPress – darauf habe ich mich spezialisiert.

Weitere Beiträge von adrian anzeigen

Hinterlasse eine Antwort

Contact us!