Automattic / jetpack

Security, performance, marketing, and design tools — Jetpack is made by WordPress experts to make WP sites safer and faster, and help you grow your traffic.
https://jetpack.com/
Other
1.59k stars 798 forks source link

Search: add better support for sites with multiple languages #8766

Open gibrown opened 6 years ago

gibrown commented 6 years ago

We index posts based on both the language set for the blog, and also the language detected for the post. Currently though the search will only search based on the blog's language setting. We should add a way to support/control cross language searching.

Two cases to handle on the search side:

There are also two popular multilanguage plugins we should add support for: WPML and Polylang. The indexing side of these falls under #8663, but on the search side we should also hook into how they handle search.

stale[bot] commented 6 years ago

This issue has been marked as stale. This happened because:

No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.

davoraltman commented 6 years ago

Requested in 5988426-hc, the website uses Polylang and it would be nice to have the search widget compatible with this plugin.

stale[bot] commented 5 years ago

This issue has been marked as stale. This happened because:

No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.

jeherve commented 4 years ago

Also in #16150.

snowmads commented 4 years ago

Requested in 3037423-zd - leaving a comment here to follow up with them if this is fixed in the future.

mdrovdahl commented 3 years ago

I'm currently working on adding JP Instant Search to a multilingual site w/Polylang and can report that things are working nicely.

What is working:

What doesn't work, but also is not promised ;-)

So, cross-language searching and filtering is working (for me, on this site, with Polylang). There's still no settings to control the cross-language search behavior...but, I think https://jetpack.com/support/search/frequently-asked-questions/#troubleshoot-multilingual could be updated to reflect that multilingual content search does work.

fredrikekelund commented 3 years ago

I spent some time trying to get Jetpack search and Polylang to play nicely.

Here's a code snippet that made it work for me:

add_filter( 'jetpack_instant_search_options', function($options) {
    $lang = pll_current_language();

    $options['adminQueryFilter'] = array(
        'bool' => array(
            'must' => array(
                array( 'term' => array( 'taxonomy.language.slug' => $lang ) ),
            )
        )
    );

    return $options;
} );
gibrown commented 3 years ago

@Automattic/jetpack-search do you think we should just add this filter whenever polylang is detected? Would WPML also work this way?

bluefuton commented 3 years ago

do you think we should just add this filter whenever polylang is detected? Would WPML also work this way?

Assuming pll_current_language() is returning the current user-selected language, it seems like a sensible default to adopt 👍

mk-kialo commented 2 years ago

I was considering using Jetpack Search for our site, but the lack of WPML support unfortunately makes it not an option. :(

github-actions[bot] commented 2 years ago

Support References

This comment is automatically generated. Please do not edit it.

jiangyanfeng commented 1 year ago

Hello,

I have a solution by add some code in theme/function.php, and it works. add_filter( 'jetpack_instant_search_options', function($options) { if ( function_exists( 'icl_object_id' ) ) { global $sitepress, $wpdb; $language_code = $sitepress->get_current_language(); $ids = $wpdb->get_col("SELECT element_id FROM {$wpdb->prefix}icl_translations WHERE element_type='post_post' AND language_code='{$language_code}'"); $options['adminQueryFilter'] = array( 'bool' => array( 'must' => array( array( 'terms' => array( 'post_id' => $ids) ), ) ) ); } return $options; });

But there's another problem: the url is not correct in the search result. For example, my English site is www.example.com and my germany site is de.example.com, when I search in the germany site, the result is all germany post, but the url of the post is still www.example.com/xxx/xxx, not de.example.com/xxx/xxx

How to fix this?

rjdusk commented 1 year ago

I spent some time trying to get Jetpack search and Polylang to play nicely.

Here's a code snippet that made it work for me:

add_filter( 'jetpack_instant_search_options', function($options) {
    $lang = pll_current_language();

    $options['adminQueryFilter'] = array(
        'bool' => array(
            'must' => array(
                array( 'term' => array( 'taxonomy.language.slug' => $lang ) ),
            )
        )
    );

    return $options;
} );

Has anyone got this code snippet to work? I've got Jetpack Search up and running on my site that uses Polylang, which then returned multiple versions of a page (language versions) within the search :( Adding this code snippet to my functions.php file resulted in nothing being returned when searching.

RavanH commented 1 year ago

Hi @rjdusk I've got this working on ontherhone.com (english and french). I should maybe note that I added the code snippet via the WPCode plugin but I would be surprised if that made a difference from adding it in functions.php ...

formosattic commented 1 year ago

+1, 6661488-zd-a8c, to have Jetpack Search working with WPML.

I had to uninstall the plugin as currently it lacks an integration with the multilingual plugin WPML that would allow to filter search results to the active language.

jeherve commented 1 year ago

This was also brought up in #32811 (cc'ing @diiegopereira, who works on WPML and opened the issue).

ajaykj commented 1 year ago

+1, 7211982-zd-a8c, to have Jetpack Search working with WPML.

formosattic commented 1 year ago

+1 7216357-zd-a8c, request for compatibility with Weglot.

formosattic commented 1 year ago

+1 6281327-zd-a8c, compatibility with WPML

nate-allen commented 7 months ago

The solutions mentioned above don't work for TranslatePress because that plugin stores the translations in a custom table, not as separate posts like Polylang.