Skip Navigation
availability:

WPML Version: 3.2.2

description:

This action hook allows controlling the language in WordPress queries. It overrides the WPML language global variable used to filter database query requests.

Note: The action hook must be passed to the pre_get_posts WordPress hook. Use with caution since it affects the WPML language variable globally. To avoid unexpected results, use the hook in combination with WordPress conditional tags to only alter the specific query on the page you want.

type:
action
category:
Retrieving Language Information for Content
parameters:
do_action( 'wpml_switch_language', string $language_code )
$language_code
(string) (Required) The 2-letter language code to switch to. If set to null it restores the original language. If set to ‘all’ it will query content from all active languages. Defaults to null
hook example usage:

In the example below, please note the use of conditional tags to control the specific WordPress query we want. For additional information on the use of conditionals please refer to the pre_get_posts page in the WordPress Codex.

Example

// using the hook on a custom query on the WordPress index front end page
function my_custom_something($query) {
    if ( !is_admin() && is_index() && !$query->is_main_query() ) {
        do_action( 'wpml_switch_language', "de" );
    }
}
add_action('pre_get_posts', 'my_custom_something');