I got some info from the 2nd tier support. Please check it below.
Currently, it's not an easy way to fix this issue. You can try to edit the file wp-content/plugins/sitepress-multilingual-cms/classes/query-filtering/class-wpml-query-parser.php line 391, change the code
from
if ( 'attachment' === $post_type || $current_language !== $this->sitepress->get_default_language() {
Hi Long. Thanks again for transmitting my issue to second tier support.
The code above is working in the test case I provided to you (thx to the 2nd tier support for that). Unfortunately I haven't been able to make it work this way in the more complex case I have, in ajax context and with other plugins.
However, while testing further, I managed to make it work with a different method. It seems it's working now for me, so I will not investigate further the problem. In case it can help anybody, I added the line
function df_multilingual_posts_filter( $query ) {
if(is_main_query() && wp_doing_ajax() && $_COOKIE['DefactoMultilanguage']=='true'){
global $sitepress;
if (class_exists('SitePress')) {
global $sitepress_settings;
$sitepress_settings['auto_adjust_ids'] = 0;
}
// Disable current hook to avoid infinite loop
remove_action( 'parse_query', 'df_multilingual_posts_filter', 1 );
remove_filter( 'get_terms_args', [ $sitepress, 'get_terms_args_filter' ], 10 );
remove_filter( 'terms_clauses', [ $sitepress, 'terms_clauses' ] );
remove_filter( 'parse_query', [ $sitepress, 'parse_query' ] );
$previous_lang = apply_filters( 'wpml_current_language', NULL );
$current_lang = 'it'; // force choice of italian
$sitepress->switch_lang($current_lang);
// Get chosen languages
$translations = arbitrary_language_switch($current_lang);
$first = $current_lang;
$second = $translations[0];
$third = $translations[1];
$excluded_posts = array();
// Get posts already in our language to exlude their translations
$tmp_args = array(
'suppress_filters' => false,
'numberposts' => -1,
'fields' => 'ids',
);
$all_posts = get_posts( $tmp_args );
foreach($all_posts as $post_id){
$tmp_result[0] = apply_filters( 'wpml_object_id', $post_id, 'post', FALSE, $second );
$tmp_result[1] = apply_filters( 'wpml_object_id', $post_id, 'post', FALSE, $third );
if($tmp_result[0]){$excluded_posts[] = $tmp_result[0];}
if($tmp_result[1]){$excluded_posts[] = $tmp_result[1];}
$tmp_result = array();
}
// Get posts in secondary language to exclude their translations in third language
$sitepress->switch_lang($second);
$tmp_args = array(
'suppress_filters' => false,
'numberposts' => -1,
'fields' => 'ids',
'post__not_in' => $excluded_posts,
);
$all_posts = get_posts( $tmp_args );
foreach($all_posts as $post_id){
$tmp_result[1] = apply_filters( 'wpml_object_id', $post_id, 'post', FALSE, $third );
if($tmp_result[1]){$excluded_posts[] = $tmp_result[1];}
$tmp_result = array();
}
add_action('parse_query','df_multilingual_posts_filter', 1);
$sitepress->switch_lang($previous_lang);
$query->set('post__not_in', $excluded_posts);
$query->set('suppress_filters', true);
}
}
add_action('parse_query','df_multilingual_posts_filter', 1);
I didn't have to alter WPML code, so I can still update easily the plugin which is a good point.
I have a last question before I close this ticket. I think having the possibility to query posts regardless of the languages should be a feature of WPML. Can you transfer this ticket as a feature request or do you prefer I write a new one ?