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: Bug, Custom Work
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 Instead, I got: Only the ones translated in Greek. The fallback feature does not work |
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. $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. |
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. 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 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: 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? 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. Look forward to your reply. |
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. 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: $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: 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. |