Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
I'm using a custom code, I want to display the translation posts in the second language and fallback posts in the original language if the translations are missing with a custom query and Ajax.
Solution:
Add the code below before the WP_Query in the Ajax callback function to show the fallback posts:

if ( class_exists( 'WPML_Display_As_Translated_Tax_Query' ) ) {
    global $sitepress, $wpml_term_translations;
    $wpml_display_as_translated_tax = new WPML_Display_As_Translated_Tax_Query( $sitepress, $wpml_term_translations );
    $wpml_display_as_translated_tax->add_hooks();
}

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.

Tagged: ,

This topic contains 15 replies, has 2 voices.

Last updated by Long Nguyen 1 year, 2 months ago.

Assisted by: Long Nguyen.

Author Posts
November 15, 2023 at 5:35 pm #14819469

Gregory

I am trying to: Use WP_Query to bring a custom post type filtered by taxonomies. I want to bring all items translated and untranslated. The latter should fallback to their original language. While in the main query it works correctly in my custom WP_Query it does not.

Link to a page where the issue can be seen: hidden link Select "Ελλάδα" and "Περιφέρεια Αττικής". Screenshot is also provided

I expected to see: The same items I see here
hidden link

Instead, I got: Only the ones translated in Greek. The fallback feature does not work

Screenshot 2023-11-15 at 19-33-45 Assets – Life Loop.png
November 17, 2023 at 6:50 am #14831921

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.

You are using a custom query to display custom posts on your site. I would like to inform you that helping you with custom code, is out of the scope of WPML. But I suggest you try with a clean installation of WordPress and with only one query to see how it goes.
I test the default query below on my demo site and see that the fallback translation works properly in the second language.

$args = [
	'post_type' => 'my-cpt',
	'tax_query' => [
		[
			'taxonomy' => 'my-taxonomy',
			'field' => 'slug',
			'terms' => 'term-1-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>' . esc_html( get_the_title() ) . '</li>';
	}
	echo '</ul>';
} else {
	esc_html_e( 'Sorry, no posts matched your criteria.' );
}
// Restore original Post Data.
wp_reset_postdata();

If you are not able to accomplish this, I would recommend you contact one of our certified partners who 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

November 21, 2023 at 2:54 pm #14888177

Gregory

Hi there,

Thank you very much for your reply.

We've tested the code you've attached, and indeed, it seems to work. It fails once we use it with WP Ajax, which is the case in our original post.

We do not ask for help with custom coding but we have to be able to do our custom coding without things failing, such as what seems to be the case with WP Ajax.
Coulld you please inform us of the standard WPML guidelines as to how to work with WP Ajax so the code you posted above still works?

Thank you very much.

November 22, 2023 at 1:30 am #14892385

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

If you use Ajax to get posts, please follow the documentation below to add the current language to Ajax parameters. Then use the action hook "wpml_switch_language" to switch to the current language or filter hook "wpml_object_id" to get the post ID or its translation in the current language.

Please follow the documentation
https://wpml.org/documentation/support/debugging-theme-compatibility/#issue-custom-non-standard-wordpress-ajax-requests-always-return-the-default-language-content
https://wpml.org/wpml-hook/wpml_object_id/

I hope I was helpful. Don't hesitate to ask if you find any problem along the way, I'll gladly help you.

November 23, 2023 at 10:59 pm #14912553

Gregory

Hello Long,

Thank you very much for the reply and for your help.

We applied the code you suggested and some variations of it but nothing changed - possibly because the documentation seems to try do address a different problem:
the issue addressed there is that "Custom (non-standard WordPress) AJAX requests always return the default language content", whereas the issue we're facing is that the AJAX request only returns the translated items and not the untranslated ones (for a given taxonomy). So we're actually "missing" the items in the default language.

Also, we are using the standard WP AJAX url (admin_url( 'admin-ajax.php' )), not our own.

Thank you very much

November 24, 2023 at 2:12 am #14912981

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

As our support policy, we don't support the custom code. I recommend you contact one of our certified partners who 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/

Thanks for your understanding and patience.

November 24, 2023 at 8:25 am #14913913

Gregory

Hi,

Thanky you for your reply.

As already mentioned, we are not using custom AJAX, but regular WP AJAX. How come there is documentation for custom AJAX urls but not for standard WP AJAX?

As also already mentioned, our problem is not with the custom code. The problem is that WPML doesn't function the way it claims it does - namely, to always display both translated and untranslated posts when this setting is selected. This is a setting in the plugin and nowhere is it stated that it doesn't function with AJAX.

Lookign forward to your reply.

November 24, 2023 at 10:21 am #14915799

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

The solution in the documentation works with WP Ajax also, when you use a custom query to get posts.

our problem is not with the custom code

Isn't this a custom code as you mentioned in the first comment?
"While in the main query it works correctly in my custom WP_Query it does not"

If the Ajax load is a default setting of the commercial theme or a plugin, can you please help me replicate the issue on a clean installation of WordPress? With this link, you can login directly to the admin area hidden link

Kindly follow the steps below:

- Don’t restore your site there.
- Install the theme or plugin that has the Ajax load on your site.
- Try to replicate the issue with minimal steps.
- Then let me know the steps to replicate it.

Look forward to your reply.
Thanks

November 30, 2023 at 2:51 pm #14964211

Gregory

Hi there,

Thank you for your reply and for providing a sandbox installation.

What we mean by "our problem is not with the custom code" is that we don't ask for assistance in writing our own custom code. The issue seems to have arisen when, code that was fully functional without WPML doesn't work as expected according to WPML.

As the theme and plugin used in the project are rather complex, and as we trully do not want to waste any of your time, we tried to create a plugin as simple as we could think of in order to display the issue.
The plugin is "Ajax Test Plugin", where there is a shortcode and a basic Ajax call with the taxonomy terms hard-coded into the Query.
As you will see, the shortcode loads all posts belonging to the translated taxonomy term, but only the translated ones when the Ajax is called. It can be seen in action here:
hidden link

Thank you very much

December 1, 2023 at 3:26 am #14968271

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

I found a way to display the fallback translation with Ajax in your case. You can check this in the plugin file:
- Switch the language to the default one English.
- Use the filter hook "wpml_object_id", get the translated content in the current language and return the original if missing.

$post_id = apply_filters( 'wpml_object_id', $post_id, 'asset', TRUE, $_GET[ 'wpml_lang' ] );

Refer documentation https://wpml.org/wpml-hook/wpml_object_id/

Let me know if it helps.

December 6, 2023 at 9:28 pm #15012411

Gregory

Hi there,

Thank you very much for looking into this. We've run some tests and, although it does work for the initial case, there are two cases we could trace where id doesn't seem to work:
1. With quering the parent category (a category where its children categories are the ones which also have posts).
2. With getting posts that exist only in the queried language. This means posts that do not exist in the primary language, but only in the secondary one (which is being queried).
We have tried the above in the sandbox to confirm that the call does not seem to work.

Thank you very much

December 7, 2023 at 3:29 am #15013005

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

Thanks for the details. I'm asking our 2nd tier support about this issue and I will update you here when I have something to share.

Thanks.

December 7, 2023 at 11:34 am #15016169

Gregory

Thank you very much!

December 15, 2023 at 1:49 pm #15073451

Gregory

Could you please give us some feedback on how this is going?

December 18, 2023 at 1:11 am #15079859

Long Nguyen
Supporter

Languages: English (English )

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

Hi,

This issue has been escalated to our development team and currently, I don't have any extra information. I will update you here once I have something to share.

Thanks.