Thanks for writing back.
With WPML, the code needed to generate the options dynamically will become slightly more complicated. That is because the posts in the different languages in that post type for options will have different post IDs.
You'll have to make sure that those selected custom fields always use the default/primary language's post ID as an option value, but the title that is generated for those options uses the current language's post title.
For example, suppose you have 3 select-type custom fields with titles 'Select Field 1', 'Select Field 2', & 'Select Field 3' and in those fields, you'd like to generate the options from the post type with slug 'book'. The code, in this case, will look like this:
add_filter( 'wpt_field_options', 'custom_populate_select_field_options', 10, 3);
function custom_populate_select_field_options( $options, $title, $type ){
switch( $title ){
case 'Select Field 1':
case 'Select Field 2':
case 'Select Field 3':
// get primary language code
$default_lang = apply_filters('wpml_default_language', NULL );
// get current language code
$current_lang = apply_filters( 'wpml_current_language', NULL );
// get the list of posts from the target post type
$main_args = array(
'post_type' => 'book',
'posts_per_page' => -1,
'post_status' => 'publish',
);
$posts_array = get_posts( $main_args );
foreach ($posts_array as $post) {
$args = array('element_id' => $post->ID, 'element_type' => 'post' );
$post_language_code = apply_filters( 'wpml_element_language_code', null, $args );
// make sure only the posts in the primary language are included as options
if($default_lang == $post_language_code) {
$options[] = array(
'#value' => $post->ID,
'#title' => get_the_title(apply_filters( 'wpml_object_id', $post->ID, 'post', FALSE, $current_lang )),
);
}
}
break;
}
return $options;
}
The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.
Notes:
1. You'll need to adjust the target select type field slugs and the target custom post type's slug in this code snippet, as per your website.
2. In these select-type custom fields, please make sure that the option 'Copy from original to translation' is selected, so that they have the same value in the translated posts.