Skip Navigation

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

Problem:
The client is trying to retrieve all posts for a single language code using WP_Query and WPML filters, specifically for the default language code 'en'. The provided code with the

add_filter('wpml_query_filter', 'filter_wpml_query_for_english')

and the custom function did not work, as it still retrieved posts in all languages.

Solution:
We recommend using

'suppress_filters' => false

in the WP_Query arguments. This will ensure that the query retrieves posts in the current language when on the English version of the website. For further optimization, the client can specify the fields they want to retrieve, such as 'ID', 'post_title', and 'post_content'.
For more information on debugging theme compatibility with WPML, please visit: Debugging theme compatibility with WPML.
Additionally, for WPML hooks reference, check out: WPML hooks reference.
For WordPress reference on get_posts, please see: WordPress get_posts function.

If this solution doesn't look relevant, please open a new support ticket in our support forum.

100% of people find this useful.

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 Andrey 1 year, 5 months ago.

Assisted by: Andrey.

Author Posts
November 22, 2023 at 12:37 pm #14897515

jean-marcd-2

Hello,
We would like to retrieve all POSTs for a single language code using WP_Query and potential WPML filters. In this case the default language code 'en'.

We tried: add_filter( 'wpml_query_filter', 'filter_wpml_query_for_english' );
with the function:
// Custom function to filter WPML query for English posts
function filter_wpml_query_for_english( $query_args ) {
$query_args['language'] = 'en'; // Change 'en' to the desired language code
return $query_args;
}

But it does not work! Still ALL Posts in all language are retrieved.

We do not want to get all POSTs in all languages and then loop through to find out which one are in 'en'. As there are many POSTs this is taking too much time.

If we could retrieve all 'en' POSTs with specific selected fields like:
'fields' => Array ('ID','post_title','post_content')

This would be perfect to optimize our get POSTs query.

Is there an easy way to do this?

Thank you.

November 22, 2023 at 8:28 pm #14902547

Andrey
WPML Supporter since 06/2013

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kyiv (GMT+03:00)

Thank you for contacting WPML support.

There is no language code to use as an argument, but instead, you can use 'suppress_filters' => false with your WP_Query. This will retrieve all the English posts if you are on the Engish version of the website.

Have a look here: https://wpml.org/documentation/support/debugging-theme-compatibility/#issue-wp_queryargs-or-get_postsargs-doesnt-filter-out-correct-posts-ids-for-the-current-language

In case you need WPML hooks: https://wpml.org/documentation/support/wpml-coding-api/wpml-hooks-reference/

WP: https://developer.wordpress.org/reference/functions/get_posts/

I hope this helps.