Polylang & Advanced Custom Fields
Polylang is the most popular multilingual plugin available on the WordPress repository. As per their stats, there are 700,000+ users were installed this plugin on their sites.
On the other hand, Advanced Custom Fields aka ACF is a wildly used plugin that allows you to take full control of the WordPress edit screen and tailor the content editing experience around your website’s needs!
Based on our client’s requirement we have installed both of these plugins on a site.
Extension details:
Polylang | Advanced Custom Fields | |
---|---|---|
Type | Free Plugin | PRO Plugin |
Version | 3.3.1 | 5.8.1 |
Tested WordPress version | 6.1.1 | 6.1.1 |
PHP version | 8.1.10 | 8.1.10 |
Languages used | Deutsch and Svenska | Deutsch and Svenska |
Issue details:
While using both plugins together, we had a backend issue when creating an ACF relationship field.
By default, the ACF relationship field does not filter the contents based on the current page language. But if we use the search feature of the relationship field, it will filter items based on the current page language.
Solution:
Just add the following code to your functions.php file to solve this issue. Now the ACF relationship field will listen to the current page language and show the data based on the language.
Add Polylang support to the ACF:
add_filter('acf/fields/relationship/query', 'aam_acf_fields_relationship_query', 10, 3);
function aam_acf_fields_relationship_query( $args, $field, $post_id ) {
$current_language_slug = pll_get_post_language($post_id, 'slug');
$args['lang'] = $current_language_slug;
return $args;
}
Other possible solutions:
// add filter with the path to your acf installation
add_filter('acf/settings/default_language', 'aam_settings_default_language');
add_filter('acf/settings/current_language', 'aam_settings_current_language');
function aam_settings_default_language( $lang ) {
if($lang == "") {
$lang = pll_default_language(); // pll_ is a polylang function
}
return $lang;
}
function aam_settings_current_language( $lang ) {
$lang = pll_current_language();
return $lang;
}
(Image from happyaddons.com customized)
Leave a Reply
You must be logged in to post a comment.