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.

This topic contains 8 replies, has 2 voices.

Last updated by Ricardo Alday 1 year ago.

Assigned support staff: Ricardo Alday.

Author Posts
September 13, 2018 at 12:50 am #2729253

carecweb

Hello WPML Support,

We have an English site which uses WP_Query heavily in code. I would like to understand the behavior of WPML with WP_Query, and hopefully minimize code updates when making our site multilingual:

1) Does WP_Query return the translated results automatically (e.g. English WP_Query args will return translated posts) or do you need to translate your WP_Query arguments (e.g. post_ID, post_name, post_title, taxonomy) first before supplying to WP_Query?

2) If WP_Query can do both (i.e. return original or translated results), what is its default behavior? Can you set or override this behavior in code? For example, we are thinking of having different configuration files per language, each containing a different set of post ids and taxonomy ids to use for certain WP_Query calls.

3) When returning translated results, is there an option to set whether to return nothing or the original English post when no translation exists? Will the settings in WPML->settings affect not only the main query but WP_Query results as well?

4) In relation to previous question, can you override or specify this behavior in code (what to return when no translation exists)

Thanks

September 14, 2018 at 8:19 pm #2736648

Ricardo Alday

Hi,

Thank you for contacting support.
By default WordPress uses the argument suppress_filters => true for the get_posts() function (used by WP_Query).
This causes WPML filters not to be called and posts in all languages are returned.

One way to bypass it is to pass the suppress_filters => false to the query arguments or don't pass it when you want results in all languages.

By default, if suppress_filters => false is passed and no translation exists, it will not return results. You must also have the queried post type or taxonomy set to "Translatable - only show translated items"

Hope that helps,
Ricardo

September 16, 2018 at 11:58 pm #2739206

carecweb

Thanks Ricardo, however I don't understand how to use "suppress_filters" for WP_Query. I've looked at https://codex.wordpress.org/Class_Reference/WP_Query but I can't in there how to use "suppress_filters". Can you provide a snippet for that and what it does?

Upon my research from your developer documentation and support blogs, I came about WPML->Settings->"Adjust IDs for multilingual functionality" which seems easier to use than "suppress_filters". I tried checking and unchecking (and then updating permalinks to be sure) and the executing WP_Query but the results in both cases are the same regardless.

Here are the results. EN is default while RU is the translated site. Post_id=310 is the original EN post, while post_id=610 is the RU translation.

1) hidden link

WP_Query( array( 'p' => 310 ) ) retrieves 310 EN post
WP_Query( array( 'p' => 610 ) ) retrieves 610 RU post

2) hidden link

WP_Query( array( 'p' => 310 ) ) retrieves 610 RU post
WP_Query( array( 'p' => 610 ) ) retrieves 610 RU post

Is the translation of the 310 EN post from the Russian site (number 2) the default behavior? How can you specify in code to return the result as is, without translation? Should the "Adjust IDs for multilingual functionality" affect or change this behavior? Can you override this setting in code?

Please also advise how the "suppress_filters" and "Adjust IDs..." are related and when to use each.

Thanks!

September 17, 2018 at 11:14 pm #2742775

Ricardo Alday

When you use the "Adjust IDs for multilingual functionality" option, WPML will automatically try to convert IDs from the default language to the current language.
If your theme has hardcoded IDs, or you specify IDs in the Admin panel, it will make those IDs adapt per language.

By default WordPress uses the argument suppress_filters => true for the get_posts() function.
This causes WPML filters not to be called and posts in all languages are returned.

To use the filter simply add it as a parameter to your query. For example:
WP_Query( array( 'p' => 310, 'suppress_filters' => false ) )

Hope that helps.

September 23, 2018 at 9:12 am #2758416

carecweb

I included the "suppress_filters" as an argument of WP_Query as you advised. Looking at the resulting SQL query, suppress_filters="false" adds a condition that the posts returned should belong to the current language.

However this still does not answer my problem in example 2. When in the translated site (RU), WP_Query( array( 'p' => 310, 'suppress_filters'=>$suppress_filters ) ) still passes the equivalent translated id (610) into the query, not the specific ID passed (310), regardless of the value of $suppress_filters.

This does not happen in the default site (EN), where the ID passed in WP_Query is the actual ID used in the query.

How can I set whether the post_id passed in WP_Query should be translated to its equivalent id or passed as is?

As of now, I not able to retrieve EN posts when I'm in the RU site. And the "suppress_filters" wasn't able to change this behavior.

In addition, "Adjust IDs..." has no effect also, whether checked or not, whether "suppress_filters" is true or false. I understand in theory what this setting does, but in my case I haven't seen its effect on WP_Query. Can you also clarify what this option does and provide examples?

Thanks again.

September 25, 2018 at 6:32 pm #2765004

Ricardo Alday

Hi,

I just wanted to let you know that I ran some tests and I can confirm the issue you're referring to. I'm consulting with our 2nd tier support for more information. I will update you as soon as I have some feedback from them.

Thanks for your patience.

September 27, 2018 at 3:12 pm #2771007

Ricardo Alday

Thanks for your patience. I got some feedback from our 2nd tier support.
The IDs are adjusted on parse_query filter which not suppressed when suppress_filters is true therefore, you see translated post.

You can use our API and switch to the post language then switch back to the current language.

Here is the example how to do it.

    $my_current_lang = apply_filters( 'wpml_current_language', NULL ); //Store current language
    
    do_action( 'wpml_switch_language', 'en' ); //Switch to the language in which post exist
    
    $test_q = new WP_Query(array(
    'page_id' => 2,
    ));
    
    if ($test_q->have_posts()) {
    while ($test_q->have_posts()) {
        $test_q->the_post();
        the_title();
        the_content();
    }
    }
    
    do_action( 'wpml_switch_language', $my_current_lang ); //Switch again to current language

Hope that helps.

October 9, 2018 at 4:41 am #2798235

carecweb

Thanks Ricardo,

Switching the language manually should be the solution to getting the desired post.

My other pending question is what the "Adjust IDs for multilingual functionality" is for, since upon tests it does nothing. Switching the language manually via code does the work that this setting supposedly (as stated in the documentation) automatically does.

So is this a bug or does "Adjust IDs for multilingual functionality" do something else?

October 9, 2018 at 3:24 pm #2800800

Ricardo Alday

With "Adjust IDs for multilingual functionality", the IDs are adjusted on parse_query filter which not suppressed when suppress_filters is true but by default, WordPress uses the argument suppress_filters => true for the get_posts() function.
This causes WPML filters not to be called and posts in all languages are returned.

This issue was reported internally but since it's not really a bug in WPML, we recommend the above method instead.

The topic ‘[Closed] Understanding WP_Query translation’ is closed to new replies.