Skip Navigation

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 -
- 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 18 replies, has 2 voices.

Last updated by Long Nguyen 1 year ago.

Assisted by: Long Nguyen.

Author Posts
March 9, 2023 at 2:09 am #13202621

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

Thanks for your feedback.

I'm asking my 2nd tier support about this issue and I will update you here once I have something to share.

Regards.

March 10, 2023 at 2:35 am #13212901

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

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() {

to

if ( 'attachment' === $post_type || $current_language !== $this->sitepress->get_default_language() && $q->query["suppress_filters"] !== true) {

❌ IMPORTANT: Please backup your database and website before proceeding ❌

Look forward to your reply.
Thanks

March 15, 2023 at 12:06 pm #13250869

bojanaT

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

remove_filter( 'parse_query', [ $sitepress, 'parse_query' ] );

And my full code is now :

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 ?

Best.

March 16, 2023 at 1:40 am #13255697

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

Thank you for your feedback and for sharing the solution.

I'm going to ask my 2nd tier support to consider adding a feature to query posts regardless of the languages and add this ticket a vote for that.

Thanks and have a nice day.

The topic ‘[Closed] parse_query remove other language tags even with suppress_filter’ is closed to new replies.