Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Polylang: Fix missing value on ACF relational fields
<?php
/*
Plugin Name: Polylang: Fix missing value on ACF relational fields
Plugin Description: ACF relational Field value for translated posts types where not displayed when Admin langauge differs from post language.
Author: Jörn Lund
Author URI: http://github.org/mcguffin
Version: 0.0.1
*/
/**
* Polylang addon
*/
function polylang_acf_relational_field_fix_value_display( $query ) {
global $polylang;
if ( is_admin() && ($post = get_post() ) && ( $post_lang = pll_get_post_language($post->ID) ) && pll_current_language() !== $post_lang ) {
// set
$query->set('lang',$post_lang);
}
}
function polylang_acf_relational_field_fix_on() {
add_action( 'parse_query' , 'polylang_acf_relational_field_fix_value_display' , 9 );
}
function polylang_acf_relational_field_fix_off() {
remove_action( 'parse_query' , 'polylang_acf_relational_field_fix_value_display' , 9 );
}
add_action("acf/render_field/type=post_object", 'polylang_acf_relational_field_fix_on', 9, 1);
add_action("acf/render_field/type=post_object", 'polylang_acf_relational_field_fix_off', 11, 1);
add_action("acf/render_field/type=page_link", 'polylang_acf_relational_field_fix_on', 9, 1);
add_action("acf/render_field/type=page_link", 'polylang_acf_relational_field_fix_off', 11, 1);
add_action("acf/render_field/type=relationship", 'polylang_acf_relational_field_fix_on', 9, 1);
add_action("acf/render_field/type=relationship", 'polylang_acf_relational_field_fix_off', 11, 1);
@ihorduchenko

This comment has been minimized.

Copy link

@ihorduchenko ihorduchenko commented Oct 31, 2018

Doesn't work...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment