grappler / polylang-slug

A unique post slug within the language in Polylang. This allows for a page to have same/identical slug in multiple languages.
268 stars 84 forks source link

Mixing Default langauge with current language #10

Closed heymATIN closed 8 years ago

heymATIN commented 9 years ago

I was using this code to display the default langauge for not translated languages in the current language:

add_filter('pre_get_posts', 'get_default_language_posts');

function get_default_language_posts($query) {
    if ($query->is_main_query() && function_exists('pll_default_language') && !is_admin()) {
        $terms = get_terms('post_translations'); //polylang stores translated post IDs in a serialized array in the description field of this custom taxonomy
        $defLang = pll_default_language(); //default lanuage of the blog
        $curLang = pll_current_language(); //current selected language requested on the broswer
        $filterPostIDs = array();
        foreach ($terms as $translation) {
            $transPost = unserialize($translation->description);
            //if the current language is not the default, lets pick up the default language post
            if ($defLang != $curLang)
                $filterPostIDs[] = $transPost[$defLang];
        }
        if ($defLang != $curLang) {
            $query->set('lang', $defLang . ',' . $curLang);  //select both default and current language post
            $query->set('post__not_in', $filterPostIDs); // remove the duplicate post in the default language
        }
    }
    return $query;
}

Since using your plugin it does not work anymore :/

Is it possible for you to fix it? I need both very badly!

And thanks a lot for that great plugin!!

grappler commented 9 years ago

I am looking into the code you provided to see how it works. Could you explain what is not working? What breaks?

When using the code snippet you showed do you create a dummy translate post or do you just have the default translation page?

heymATIN commented 9 years ago

I actually have no clue what happens there. I took this code from here: http://wordpress.syllogic.in/2014/08/going-multi-lingual-with-polylang/ .

I got redirected to that site from the documentation: https://polylang.wordpress.com/documentation/documentation-for-developers/tips-and-tricks/how-to-display-the-default-language-post-if-the-translation-does-not-exists/

So what that code does is that - if you do not have a translation to a post, it still shows the default translation.

Example: If I switched to the german langauge, and I have 15 posts in english, and only translated 3 posts to german. I still get all posts. 12 in english + 3 in german. Non translated posts (default english) + translated posts (german).

Edit*

And For some reason, if I use both plugins, the mixing of langauges does not work.

grappler commented 9 years ago

A temp solution would be to comment out these two lines of code in my plugin. //add_filter( 'posts_where', 'polylang_slug_posts_where_filter', 10, 2 ); //add_filter( 'posts_join', 'polylang_slug_posts_join_filter', 10, 2 );

I will try and see if there is a better solution

heymATIN commented 9 years ago

Yeah can confirm that worked! I still will check for updates. Thanks a lot. Much appreciated!

adamlindqvist commented 9 years ago

It does it job. When I want to mix two languages.

But it breaks other things and starts to redirect in a infinity loop on other pages that has the same slug.

Example: I can mix my blogposts in english and swedish. But when I have two pages: www.url.com/client/client-1 www.url.com/en/client/client-1 it starts redirecting "301 Moved Permanently" and I am not able to browse any of the page.

Is there any other way around this?

EDIT: @heymATIN @grappler This seems to solve my issue:

function polylang_slug_posts_where_filter( $where, $query ) {
global $polylang;

if ( is_admin() || ! empty( $query->query['post_type'] ) && ! $polylang->model->is_translated_post_type( $query->query['post_type'] ) ) {
    return $where;
}

 // I added this
if( !empty( $query->query['lang'] ) ) {
    return $where;
}

$lang = pll_current_language();

// " AND pll_tr.term_taxonomy_id IN (" . implode(',', $languages) . ")"
$where .= $polylang->model->where_clause( $lang, 'post' );

return $where;
}

If I set lang in the query then I shouldnt add the default one again and just return the where-query as it is.

grappler commented 8 years ago

Thank you @adamlindqvist I will be adding your fix to the next version.

@heymATIN It seems to fix your problem.