Skip Navigation

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

Last updated by Alejandro 4 years, 4 months ago.

Assisted by: Alejandro.

Author Posts
December 9, 2019 at 4:55 pm #5094505

kenM-6

Hi,

Our search results are showing posts and pages from all languages regardless of the language you're searching in.

When searching on our English (default) language, I want to see English search results.
When searching on German page, I want German search results, etc.

See the following search:
hidden link

The above search is done on our German page and I expect to see German search results. Instead, I see English search results.

This only seems to be an issue with the search results page (uses WP_Query). Other loops, such as our Blog posts, have no issues and show up fine. Only the Search Results are not respecting the current language.

Here is our search results loop:

<?php 
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$s = get_search_query();

$args = array(
   'supress_filters' => false,
   'posts_per_page' => 5,
   's' => $s,
   'paged' => $paged
 );

$search_results = new WP_Query( $args );
?>

<?php if ( $search_results->have_posts() && !empty(get_query_var('s'))) : ?>
<?php while ( $search_results->have_posts() ) : $search_results->the_post(); ?>

// Search result Post Title, Excerpt, Etc here

<?php endwhile; ?>
<?php wp_reset_postdata(); ?>

// the_posts_pagination();

<?php endif; ?>
December 11, 2019 at 8:12 am #5104317

Yvette
Supporter

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

Timezone: Europe/Paris (GMT+02:00)

Hello

Can you please read through this older thread which discusses the recommended way to use WP_query with WPML:
https://wpml.org/forums/topic/understanding-wp_query-translation/#post-2771007
https://wpml.org/forums/topic/passing-language-to-wp_query-2/

Let us know if you are still having issues after having implemented the suggested procedure in these posts.

Thanks

December 12, 2019 at 10:02 pm #5118871

kenM-6

Hi Yvette,

I've tried the solutions in the links you provided but the problem persists. As I mentioned before this seems to be a particular issue with the Search Results where we are passing the search parameter as an argument:

$s = get_search_query();
$args = array (
   // other arguements
   s = $s
);

If I remove the search argument, the results show up in their appropriate language but they are not filtered by the search term.

As an additional note, I will also mention that I have tried modifying the search form according to this documentation: https://wpml.org/documentation/support/creating-multilingual-wordpress-themes/search-form/ but that also has not helped.

December 13, 2019 at 9:18 pm #5126333

Alejandro
Supporter

Languages: English (English ) Spanish (Español ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

H ithere,

I'll take over this ticket because Yvette is not available.

Do you have some form tag somewhere, maybe in the same php fiel or somewhere else where you coudl add this: https://wpml.org/documentation/support/creating-multilingual-wordpress-themes/search-form/ ?

let me know, please.

December 13, 2019 at 11:03 pm #5126393

kenM-6

Hi,

We have tried that in our staging site here: hidden link

The search form is setup as directed in the documentation, but as I mentioned in my previous post, we do not see any improvement. Is there an additional step not mentioned int he documentation that should be taken?

December 13, 2019 at 11:45 pm #5126417

Alejandro
Supporter

Languages: English (English ) Spanish (Español ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Could you allow me access to the staging site (including FTP if possible), and please point me to the directory where the search code is, so I can give it a try and see if we can do something about it.

You can add them through the fields I just enabled for that purpose.

December 17, 2019 at 3:37 pm #5148453

Alejandro
Supporter

Languages: English (English ) Spanish (Español ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Ok, the problem here is not that the search results are not in the currently set language but instead that your code doesn't seem to be filtering by languages at all! check this video that i recorded in your site: hidden link

If you search for a word, it will not take into consideration the language, but the page is the correct one, in the correct language, the problem is with the results which are not filtering by language at all, if the results match English and German it will show all of them.

Can you check the code that has the loop for the content filtering? because the problem seems to be in there.

There is also a code that you might want to test:

 
          global $sitepress;
          $my_current_lang = apply_filters( 'wpml_current_language', NULL ); //Store current language
          var_dump($my_current_lang);
          if ((ICL_LANGUAGE_CODE=='de')) {
              $lang="de";
              $sitepress->switch_lang($lang);
              //do_action( 'wpml_switch_language', 'de' );
          } 

that's located in the search.php file.

I'm not sure what you wanted to achieve with this code, but maybe it has to do with the issue.

One last thing i noted is that you are using ACF as well, but not our glue plugin, Advanced Custom Field Multilingual. If you use ACF to somehow work with the search page, then you definitely need to use Advanced Custom Fields Multilingual and configure it as seen here: https://wpml.org/documentation/related-projects/translate-sites-built-with-acf/

Let me know how it goes.

December 19, 2019 at 7:26 pm #5166173

kenM-6

Hi, thank you for taking the time and looking into this issue. I appreciate it very much.

I'm not sure what you wanted to achieve with this code, but maybe it has to do with the issue.

That code you mention is part of our efforts to filter the search results. That is code provided in WPML documents and other support tickets.

I think providing the file with our efforts to fix the issue was a mistake and added some confusion. I've restored the search.php file to its original state. If you see the search.php file now, you will see that it uses the default WP_Query with no custom parameters. The search results behavior is still the same. This is what originally led me to believe that the issue had something to do with suppress filters (as you mentioned in your video) and why I created a custom query. But as you can see, with the default query, the problem is the same.

I also installed ACFML and looked over the settings but the problem still persists. I think perhaps taking a look at how the search results work with translated posts of custom taxonomies is a good idea, but I don't know where to look. Any other ideas?

December 20, 2019 at 9:34 am #5169633

Alejandro
Supporter

Languages: English (English ) Spanish (Español ) Italian (Italiano )

Timezone: Europe/Rome (GMT+02:00)

Could you please check the site? i tried to disable the plugins except WPML and a few others, and the site just crashed and the debug.log is not generated.

I activated everything again and the problem was still there for some reason.

Then, please revert to the search files from the original theme for a moment and make sure the WPML multilingual form code is there right before the closing <form> tag (https://wpml.org/wpml-hook/wpml_add_language_form_field/)

Then, test there if things work.

I'm asking this because i can see the problem here is with the products mostly, since when i reverted to the theme before, the products didn't appear in the search results and all the pages seemed to show correctly, in the correct language.

The products, however are always showing in English, and i'm starting to believe the problem could be that the product slug was translated and the condition you added is only triggering the english version of the form. you could try creating an if conditional with the post type in german (i suppose it's "produkt") and see if that changes anything.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.