Skip Navigation

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

Problem:
Trying to use the

wpml_switch_language

function within a filter to list posts from the default language on a page translated to another language, and can't get the

wpml_switch_language

to work.

Solution:
Firstly, it's important to note that the

wpml_switch_language

action hook must be used with the

pre_get_posts

WordPress hook. This means that you should integrate the language switch into the query modification process. Here's how you can do it:

1. Use the

pre_get_posts

hook to modify the query before it runs.
2. Inside your hooked function, call

do_action('wpml_switch_language', 'all')

to switch the language.
3. Then set up your query arguments and run the query.

For more details and usage examples, you can refer to the following documentation:

Additionally, there are alternative approaches you can consider, such as:

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 1 reply, has 2 voices.

Last updated by Waqas Bin Hasan 1 year, 7 months ago.

Assisted by: Waqas Bin Hasan.

Author Posts
November 10, 2023 at 8:34 pm #14780301

jackc-12

I'm really struggling so i hope you can help.

I cant get the wpml_switch_language function to work.

i need to list posts from en translation (default language) on a page translated for Australia en-AU.

to do this with my theme i need to contain it within a filter e.g.

add_filter( 'cs_looper_custom_service_faq', function( $result, $params) {

//my hook and query goes here///

return $query;
}, 10, 2);

So I've tried and tried - used chat gpt and all sorts but cant get it to work.

Here is what i have using: https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/?wpv_view_count=602355&wpvhookcategory=retrieving-language-information-for-content&wpv-hook=0&wpv_post_search&wpv_filter_submit=Submit#hook-662213

Is this right? Thanks

add_filter( 'cs_dynamic_content_aulist', function( $result, $field, $args) {

do_action( 'wpml_switch_language', 'all' );

$query_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'category__in' => array( 1230 ),
);

$query = new WP_Query( $query_args );

wp_reset_postdata();

return $result;
}, 10, 3 );

November 13, 2023 at 8:47 am #14786397

Waqas Bin Hasan
WPML Supporter since 05/2014

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Jack,

Thank you for contacting the support.

wpml_switch_language states that:

The action hook must be passed to the pre_get_posts WordPress hook.

See for details and usage: https://wpml.org/wpml-hook/wpml_switch_language/ and https://developer.wordpress.org/reference/hooks/pre_get_posts/.

On the other hand, you can also use different approaches like mentioned in these guides:

- https://wpml.org/wpml-hook/wpml_object_id/
- https://wpml.org/faq/how-to-get-other-languages-of-current-post/

Regards.