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, 1 month ago.

Assisted by: Long Nguyen.

Author Posts
January 31, 2023 at 2:13 am #12925963

bojanaT

I am trying to: Load posts from different languages with tags from different languages in the same query

Link to a page where the issue can be seen: <N/A> (test version is behind an institutional firewall, prod version without the intended feature is here : hidden link )

I expected to see: Posts returned

Instead, I got: No posts returned.

######

Hi,

I'm trying to load posts from different languages at once to build a feature for a client that should retrieve posts from different languages on the same page. I managed to do this for the main page in a general context with suppress_filters, but I got troubles to get the thing working in the context where a tag is defined. If it changes anything, I'm also in an ajax context, provided by the ajax-load-more plugin.

I tried to follow the var $query, and it seems to me that the bug could be coming from the parse_query function in classes/query-filtering/class-wpml-query-parser.php. I monitored the wpml_pre_parse_query and wpml_post_parse_query filters that are called in the beginning and the end of said function, and in wpml_pre_parse_query I have :

$query[query_vars][tag_slug__in]  => Array
                (
                    [0] => annee-politique-suisse
                    [1] => annee-politique-suisse-fr
                    [2] => annee-politique-suisse-it
                )

and in wpml_post_parse_query :

$query[query_vars][tag_slug__in] => Array
                (
                    [0] => annee-politique-suisse-it
                )

I would like the query to keep searching in all the 3 tags. "suppress_filters" is true in both case and I understood that it should allow search in all languages. Is it a bug ? Is there a better way to do this ?

Thanks in advance

January 31, 2023 at 7:34 am #12926755

Long Nguyen
Supporter

Languages: English (English )

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

Hi there,

Thank you for contacting WPML support, I'd be happy to help you with this issue.

It looks like you are using a custom code to display posts from different languages on a page. We do not normally offer support for custom code, but I can suggest you a few pointers:
- Please use the latest version of WPML 4.5.14. According to the debug info, the current version on your site is 4.5.8.
- Please see WPML Hooks Reference at https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/#hook-605256 for details, specially wpml_object_id. In this reference, you may find several useful hooks and examples of how to use them. I hope you may find one to best suit your needs.
If you are not able to accomplish this, I would recommend you contact one of our certified partners that will be more than happy to help you with this. In this link, you will find a list of our certified partners: https://wpml.org/contractors/

Look forward to your reply.
Thanks

January 31, 2023 at 9:43 am #12927985

bojanaT

Hello Long, thanks for your answer,

However, I don't believe it's a custom code request. What I want to know is how I can prevent WPML to filter tags according to languages in a specific query. Surely there should be a way ?

Or do you confirm that this is not intended to be a feature of WPML ?

February 1, 2023 at 2:21 am #12934225

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

Thank you for your feedback.

As I understand, the main goal here is to get the posts and their translations in the same query, so you can use the filter hook "wpml_object_id" to get the ID of the translation post and display it on the frontend in the same query. The code below is an example to display posts in both language English and Czech on the frontend.

$args = array(
	'post_type' => 'post',
	'tax_query' => array(
		array(
			'taxonomy' => 'category',
			'field'    => 'slug',
			'terms'    => 'test-category-en',
		),
	),
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
	echo '<ul>';
	while ( $the_query->have_posts() ) {
		$the_query->the_post();
		echo '<li>' . get_the_title() . '</li>';

		$translated_post = apply_filters( 'wpml_object_id', get_the_ID(), 'post' , FALSE , 'cs' );
		echo '<li>' . get_the_title( $translated_post ) . '</li>';
	}
	echo '</ul>';
} else {
	// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

WPML filters the objects in the current language so you can use this filter for the term ID also. I hope that makes sense.

February 2, 2023 at 4:08 pm #12949519

bojanaT

Thanks, but not exactly ^^

I want to get all posts on the website, regardless of their language, in the same query. I sure could do several separate queries for each language and merge the results, but then it would not work with pagination so this won't work for me. The solution you mention here above won't work either because not all posts are translated on our website, in your example articles that exist in czech but not in english would not be fetched by your query.

I already managed to do what I want when I don't need to filter by tag, just by adding the "suppress_filters" argument to the query. But when I try to also filter by tags, WPML seems to filter out the tag_slug__in argument and remove tags that are not in the current language (see my first post for details about that).

In the end, my question is quite simple : is WPML providing a way to "disable itself" during a specific query so all posts would be fetched regardless of their language status ? I had the impression that it was, with the suppress_filters argument, but maybe I'm wrong. If not, it would probably be a nice feature to have for such a big plugin ...

February 3, 2023 at 3:21 am #12951983

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

Thank you for your feedback.

I will ask my colleague if there is a way to disable WPML itself in a specific query and I will update you here once I have something to share.

Meanwhile, you can refer a workaround here https://wpml.org/forums/topic/query-returns-posts-in-all-languages/

Regards.

February 4, 2023 at 8:39 am #12960031

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

The issue here could be the setting "auto_adjust_ids", you can try to turn it off with the workaround on this ticket https://wpml.org/forums/topic/sitemap-blog-categories-product-categories/page/2/

Look forward to your reply.
Thanks

February 14, 2023 at 4:08 pm #13032905

bojanaT

Hi, thanks to have checked with your colleagues.

I tried your solution, but still without success. Here is the portion of my code where I would like to retrieve posts from all languages. Currently, it only retrieve posts in the current language.

if (class_exists('SitePress')) {
	global $sitepress_settings;
	$sitepress_settings['auto_adjust_ids'] = 0;
}

$tmp_args = array(
  'suppress_filters' => true,
  'numberposts' => -1,
  'fields' => 'ids',
  'tag_slug__in' => array('annee-politique-suisse', 'annee-politique-suisse-it', 'annee-politique-suisse-fr'),
);
$all_posts = get_posts( $tmp_args );
February 15, 2023 at 10:18 am #13038371

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

Thank you for your feedback.

In order to investigate the issue further and escalate it to my 2nd tier support, please try to replicate the issue in a sandbox site. I created a clean installation of WordPress, WPML, and all necessary WPML add-ons. You can access the WordPress dashboard using the link below:
hidden link

And let me know steps to replicate the issue.

Look forward to your reply.
Thanks

February 21, 2023 at 4:26 pm #13081339

bojanaT

Thanks,

Here are the steps I followed on your environment :

- I set up WPML with two languages, en and fr
- I created a tag in english with it's french translation
- I created a post in english with it's french translation
- I tagged both posts with previously created tags
- I created a simple custom plugin that enable a shortcode for my tests (see code below)
- I created a page that call said shortcode on the following address : hidden link

Here is the code executed by the shortcode :

	if (class_exists('SitePress')) {
	    global $sitepress_settings;
	    $sitepress_settings['auto_adjust_ids'] = 0;
	}
	 
	$tmp_args = array(
	  'suppress_filters' => true,
	  'numberposts' => -1,
	  'fields' => 'ids',
	);
	$all_posts = get_posts( $tmp_args );
	
	echo "<h4>Without tags</h4>";
	echo "<pre>";
	print_r($all_posts);
	echo "</pre>";
	 
	$tmp_args = array(
	  'suppress_filters' => true,
	  'numberposts' => -1,
	  'fields' => 'ids',
	  'tag_slug__in' => array('test-tag-fr', 'test-tag-en'),
	);
	$all_posts = get_posts( $tmp_args );
	
	echo "<h4>With tags</h4>";
	echo "<pre>";
	print_r($all_posts);
	echo "</pre>";

As explained before, I would like to retrieve both posts in both case, since I specify all tags in tag_slug__in. In the first case without the tags, the result is correct with both ids fetched. However in the second case, only the post in current language is retrieved.

Thanks again for your help.

February 22, 2023 at 4:50 am #13084831

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

Thank you for your feedback.

I've escalated this issue to our development team. I'll update you as soon as I have something to share.

Regards.

February 22, 2023 at 10:11 am #13087081

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

I got some information from our 2nd tier support, please check this below.

Please use the code below to remove the filter term by language before getting posts:

	global $sitepress;
	remove_filter( 'get_terms_args', [ $sitepress, 'get_terms_args_filter' ], 10 );
    remove_filter( 'terms_clauses', [ $sitepress, 'terms_clauses' ] );

so the completed code would be:

	global $sitepress;
	if (class_exists('SitePress')) {
	    global $sitepress_settings;
	    $sitepress_settings['auto_adjust_ids'] = 0;
	}
	 
	$tmp_args = array(
	  'suppress_filters' => true,
	  'numberposts' => -1,
	  'fields' => 'ids',
	  'tag_slug__in' => array('test-tag-fr', 'test-tag-en'),
	);
	
	remove_filter( 'get_terms_args', [ $sitepress, 'get_terms_args_filter' ], 10 );
    remove_filter( 'terms_clauses', [ $sitepress, 'terms_clauses' ] );
	$all_posts = get_posts( $tmp_args );

Let me know if it works on your end.

March 7, 2023 at 3:44 pm #13191303

bojanaT

Hi, it's going in the right direction, but unfortunately it's still not working.

The sandbox has expired so I created a fresh environment on our server with the following settings :

- WordPress 6.1.1 (latest)
- WPML 4.6.0 (latest)
- Twenty Twenty-Three 1.0 (latest)
- No other plugins
- No other themes

Like in the sandbox, I created a post with it's translation, a tag with it's translation and uploaded a custom plugin that implement your code as a shortcode.

Your workaround is working when you are in the main language (en in this case) but not in a secondary language (fr in this case). You can see the result here : hidden link

March 8, 2023 at 8:47 am #13196623

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

Thank you for your feedback.

Can you please help me to replicate the issue on a new sandbox site? The old one is deleted due to inactivity for a while. You can login with this link
hidden link

I will ask our 2nd tier support about the issue again.

Look forward to your reply.
Thanks

March 8, 2023 at 9:38 am #13197023

bojanaT

Done. I followed the same procedure than last sandbox, with the code updated with your indications above.

As said, in english the two posts are retrieved : hidden link
But in french, only the french post is retrieved : hidden link

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