Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Localized posts redirect to incorrect language #68

Open
villesiltala opened this issue May 20, 2020 · 0 comments
Open

Localized posts redirect to incorrect language #68

villesiltala opened this issue May 20, 2020 · 0 comments
Labels
Polylang Issues related to Polylang

Comments

@villesiltala
Copy link

villesiltala commented May 20, 2020

If a post is localized to the default language, Polylang does not include a 'lang' parameter in a singular main query with URL modification settings described in the attached screenshot. This causes a redirect to incorrect language if there are localized posts with the same post name and the query randomly matches to an indexed post localized to other than the default language.

This could be fixed by filtering the WP query before RediSearch does its query and adding the missing language parameter. Here's an example of adding the missing locale:

\add_action( 'redipress/before_search',
    /**
     * Fix localization for main queries on default language.
     *
     * @param Search    $search   The search instance.
     * @param \WP_Query $wp_query The query instance.
     */
    function( $search, $wp_query ) {
        if ( $wp_query->is_main_query() ) {
            // If this is not a singular page, bail early.
            if ( ! $wp_query->is_singular ) {
                return;
            }

            // If a language is set in query vars, bail early.
            if ( ! empty( $wp_query->query_vars['lang'] ) || ! empty( $wp_query->query['lang'] ) ) {
                return;
            }

            // If the post type is not translated, bail early.
            if (
                function_exists( 'pll_is_translated_post_type' ) &&
                ! pll_is_translated_post_type( $wp_query->get( 'post_type' ) )
            ) {
                return;
            }

            $tax_queries = $wp_query->get( 'tax_query' ) ?: [];

            // Try to find the language query.
            $lang_set = false;
            foreach ( $tax_queries as $idx => $tax_query ) {
                $taxonomy = $tax_query['taxonomy'] ?? '';
                if ( $taxonomy === 'language' ) {
                    $lang_set = true;
                }
            }

            // If no lang set, set the default language.
            if ( ! $lang_set && function_exists( 'pll_default_language' ) ) {
                $tax_queries[] = [
                    'taxonomy' => 'language',
                    'field'    => 'slug',
                    'terms'    => [ pll_default_language() ],
                    'operator' => 'IN',
                ];
            }

            // Recreate the tax queries.
            $wp_query->tax_query = new \WP_Tax_Query( $tax_queries );
        }
    },
    1, 2
);

URL modifications settings
Screenshot 2020-05-20 15 43 06

@villesiltala villesiltala added the Polylang Issues related to Polylang label May 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Polylang Issues related to Polylang
Projects
None yet
Development

No branches or pull requests

1 participant