msaari / relevanssi-live-ajax-search

[WordPress Plugin] Enhance your search forms with live search
16 stars 2 forks source link

Multilanguage support (polylang) #1

Closed nikolip closed 2 years ago

nikolip commented 2 years ago

Hi,

It seems that currently ajax search is not working with sites that uses polylang? Or if it should work then I have a bug.

All the results are or have links to the main language side of the page.

https://discgolf.monster/ site I'm developing.

msaari commented 2 years ago

On my test site, Polylang works fine. If I'm on an English page, the search query automatically includes the language parameter for English, and on a Finnish page, the Finnish language parameter is set. Thus, the results are always in the correct language. The AJAX search does not get any extra parameters, just the search term, so somehow Polylang automatically picks up the correct language and sets the taxonomy parameters.

On your site, for some reason, the language parameter is not passed correctly, and instead, the search gets the main language parameter all the time. Polylang adds the language parameter in a parse_query action, and that should always happen in these searches.

So, I'd guess there are three options here:

  1. Polylang doesn't add the language parameter.
  2. Polylang adds the wrong language parameter.
  3. Polylang adds the language parameter and something removes it or changes it.

In any case, Live Ajax Search does not care, because all it does is pass the search query to the search, and Relevanssi searches using the parameters it's getting. Polylang should do its magic, but for some reason, it's not.

What you can do here is to check the parameters Relevanssi is getting:

add_filter( 'relevanssi_modify_wp_query', function( $query ) {
    error_log( wp_json_encode( $query ) );
    return $query;
} );

There should be a taxonomy parameter like this: "tax_query":[{"taxonomy":"language","field":"term_taxonomy_id","terms":19,"operator":"IN"}]. Is there?

nikolip commented 2 years ago

I did some quick testing when I noticed your message. Query actualy does have the parameter. "tax_query": [ { "taxonomy": "language", "field": "term_taxonomy_id", "terms": 32, "operator": "IN" } ],

I'll debug this more on upcoming week. If you have any ideas or suggestions I'm happy to take them. I'll start striping other plugins and checking my own code and see why this might happen.

Just to make sure is there specific things to check on the settings page of relevanssi (admin side) that I have missed and might cause this?

edit 1: I want to point out that relevanssi shows correct posts on search page. So my problem only occurs on ajax search.

msaari commented 2 years ago

Nope, since you're getting the correct results from the search results page, your settings must be correct (and the only setting that does matter, in any case, is the Polylang "Allow all languages" setting).

The relevanssi_live_search_query_args filter hook filters the live search query parameters. Is the language parameter included there? If not, you can add it there (or correct it, if it exists and points to the wrong language) and see if that helps.

nikolip commented 2 years ago

There was no "lang" argument in query args in "relevanssi_live_search_query_args" filter hook and when I tried to add that manualy it did nothing. eg. add_filter('relevanssi_live_search_query_args', function($args) { if(function_exists('pll_current_language')) { $lang = pll_current_language(); $args = array_merge($args, ['lang'=> $lang]); } return $args; });

I started to look into polylang settings. When I enabled "Detect browser language"-setting it seems like it started to work but actualy this shows only english posts (my browser language is english). Basicly now the problem just changes so that I can only see english posts and not finnish at all.

I'll keep debugging more when I got spare time.

msaari commented 2 years ago

What if you detect the language and then set the tax_query directly to use the correct language? Does that work?

nikolip commented 2 years ago

Okay so the situation is now the following. If I don't enable the browser detection the pll_current_lang returns allways 'fi' If I enable it it allways returns 'en' --> So basicly adding lang parameter to args with 'relevanssi_live_search_query_args' works. Now the problem is to get the correct language in that filter from polylang.

And when you look into polylang pll_get_current_language function `/**

msaari commented 2 years ago

No, but I don't know Polylang. I would recommend turning to Polylang support or some forum where there are people who use Polylang (WordPress Finland Slack, for example), as this seems to be a Polylang problem.

nikolip commented 2 years ago

Okay,

just quick update. I've been in contact to polylang support and I think they actualy find out another bug but not the reason I'm having problems with ajax. Following is taken from email I got from polylang support. " I've recorded a test I've done, I hope it's the same behaviour you have.

When I have : WordPress 5.9 Polylang 3.1.4 Polylang for WooCommerce 1.6.3 WooCommerce 6.1 The search result is well and separate by language.

When I activate Relevanssi (without the Ajax add-on), the search result is wrong, showing content in all languages. " And I have a video of the problem he discovered. Let me know if you want to see it, I can send it to your email.


But back to my problem. I've been debugging this and it seems that issue is occuring because of my wordpress template I'm using ( https://github.com/Seravo/wordpress ).

I tested to create an site with localwp and added the following plugins. WordPress 5.9 Polylang 3.1.4 Polylang for WooCommerce 1.6.3 WooCommerce 6.1

I couldn't replicate the issue. Then created a new site with seravo template and the issue is there. So I would assume that there is something wrong in the template I'm using? Sounds weird but it seems so.

I would say that you can close this issue. I've managed to create working searches with new site I created and that proves that this issue is not related to this plugin.

But still, if you have an ideas/suggestions why that template is causing the issue I'm happy to hear. :)

msaari commented 2 years ago

Hmm, I wonder if the Polylang people have the "Allow all languages" setting on in Relevanssi =D If Polylang for Woocommerce is involved somehow, I can't do much, as I don't have access to it. Polylang in itself works correctly with Relevanssi, that much I can tell for sure. If the video has something useful other than "see this does not work", then yeah, I'm interested.

That problem with the Seravo template is weird; I know it's well done, and I don't really know how it would mess with this. Perhaps Seravo support can help you out there?

nikolip commented 2 years ago

I was losing my mind so couple times and made sure that I didn't have that option enabled... :D But yeah, video was only showing the problem so that wouldn't be that helpful to you.

I agree with that Seravo template is well done. I've been in contact to seravo and we will see what they respond to me.

Thank you so much for your help so far!

msaari commented 2 years ago

I found this in my WooCommerce documentation:

"WooCommerce and Polylang don’t always play nice together. Both use tax queries – WooCommerce to hide some products, Polylang to filter the languages – and these two can clash bad. In this clash WooCommerce overrides Polylang, and the language filtering won’t work the way you want it to work.

If you’re running into problems where your language filtering is not working properly, add this to your theme functions.php:

add_filter( 'relevanssi_modify_wp_query', 'rlv_reinforce_polylang' );
function rlv_reinforce_polylang( $query ) {
    $tax_query = $query->query_vars['tax_query'];
    $tax_query[] = array(
        'taxonomy' => 'language',
        'field'    => 'slug',
        'terms'    => array( $query->query_vars['lang'] ),
    );
    $query->query_vars['tax_query'] = $tax_query;
    return $query;
}

This will take the language parameter from the lang parameter (where it works, if WooCommerce is not involved) and adds it to the tax_query.

nikolip commented 2 years ago

I actualy tested that before submitting this issue it was giving me "undefined index 'tax_query' from $tax_query = $query->query_vars['tax_query'];

msaari commented 2 years ago

Ok. I have no idea if that code is generally useful anymore, it's pretty old.

nikolip commented 2 years ago

I'll still test this once more when I have time. Just to make sure I didn't make mistakes