Skip Navigation

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

Problem:
The client is using WPML and SearchWP on their development site and wants to search for text and return posts in the current language only. However, the search returns posts in all languages.
Solution:
We recommend modifying the search query to ensure it filters results based on the current language. Here's a step-by-step guide:

// Ensure you have WPML's language set up
$current_language = apply_filters('wpml_current_language', null);
// Arguments for the query
$args = array(
    'post_type' => $post_type,
    'posts_per_page' => $posts_per_page,
    'post_status' => 'publish',
    'orderby' => $params['orderby'] ?? 'date',
    'order' => $params['order'] ?? 'DESC',
    's' => $params['search'],
    'paged' => $paged,
    'suppress_filters' => false, 
// This ensures WPML filters are applied
);
// Run the query
$query = new WP_Query($args);
// Use the results as needed
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        // Your loop content
    }
    wp_reset_postdata();
}

If this solution does not resolve your issue or seems outdated, please check the related known issues, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. If further assistance is needed, we highly recommend opening a new support ticket at WPML support forum.

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 0 voices.

Last updated by Andreas W. 1 month, 1 week ago.

Assisted by: Andreas W..

Author Posts
November 15, 2024 at 4:26 am #16405652

Devops Takeoff

Background of the issue:
I am using WPML and SearchWP on a site under development. I want to search for text and return posts in the current language only. I have been using the following code: $args = array( 'post_type' => $post_type, 'posts_per_page' => $posts_per_page, 'post_status' => 'publish', 'orderby' => $params['orderby'] ?? 'date', 'order' => $params['order'] ?? 'DESC', 's' => $params['search'], 'paged' => $paged, 'suppress_filters' => false, // 'tax_query' => array( // array( // 'taxonomy' => 'language', // 'field' => 'slug', // 'terms' => $current_language, // 'operator' => 'IN', // ), // ), // 'meta_query' => array( // array( // 'key' => 'icl_language_code', // 'value' => $current_language, // 'compare' => '=' // ) // ) ); $query = new SWP_Query($args);

Symptoms:
When I search some text, it returns all posts of all languages instead of just the current language.

Questions:
How to fix the search to return posts of the current language only?

November 18, 2024 at 9:47 pm #16416650

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

Could you please try this approach instead?

// Ensure you have WPML's language set up
$current_language = apply_filters('wpml_current_language', null);

// Arguments for the query
$args = array(
    'post_type' => $post_type,
    'posts_per_page' => $posts_per_page,
    'post_status' => 'publish',
    'orderby' => $params['orderby'] ?? 'date',
    'order' => $params['order'] ?? 'DESC',
    's' => $params['search'],
    'paged' => $paged,
    'suppress_filters' => false, // This ensures WPML filters are applied
);

// Run the query
$query = new WP_Query($args);

// Use the results as needed
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        // Your loop content
    }
    wp_reset_postdata();
}

Let me know if further assistance will be needed.

Best regards
Andreas