WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Query all language terms (26 posts)

  1. RavanH
    Member
    Posted 1 year ago #

    Hi Chouby,

    In the documentation, I read :

    Is it possible to query multiple languages?
    Yes. Example:

    $posts = get_posts(array(
        'post_type' => 'post',
        'lang' => 'de,fr', // query German and French posts
        'showposts' => 5
    ));

    I have two questions (which might have already been asked and answered but the WordPress support forums are impossible to search through) :

    1. Is there a way to include all languages without knowing them?
    Something like the parameter 'lang' => 'all' (or a *, or empty)... If not, is there maybe a constant that can be set just like for WP Super Cache will be switched off whenever the constant DONOTCACHEPAGE is set to 1 ?

    2. Is there something similar for get_terms() to query all language terms?
    This does not seem to work :

    $terms = get_terms(get_query_var('taxonomy'), array(
        'orderby' => 'count',
        'order' => 'DESC',
        'lang' => 'de,fr', // query German and French posts
        'hierachical' => 0,
        'number' => 50000
    ));

    and pll_get_term() does not cut it since I need all terms including non-translated ones.

    Or maybe there is a way to loop through all site languages and switch to each one dynamically, each time running get_terms() ?

    http://wordpress.org/extend/plugins/polylang/

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Hello RavanH!

    You can use:

    global $polylang;
    foreach ($polylang->get_languages_list() as $term)
        $langs[] = $term->slug;
    
    $posts = get_posts(array(
        'post_type' => 'post',
        'lang' => implode(',', $langs),
        'showposts' => 5
    ));
  3. RavanH
    Member
    Posted 1 year ago #

    OK thanks :)

    And for get_terms() could that be done in a similar way?

  4. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Sorry, I missed that one. Clearly Polylang does not provide an easy way to do this for the moment. I will come back with a solution.

  5. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Please try the new development version (0.9.5.3). It should now be possible to get terms by language just as you expect in the code in your initial post.
    http://downloads.wordpress.org/plugin/polylang.zip

  6. RavanH
    Member
    Posted 1 year ago #

    That's fantastic! I'll be testing it :)

    I have a suggestion: would it not have some logic to allow for a wildcard in the 'lang' array key value?

    Or rather:
    1. when the lang key is not set, Polylang filters posts (or terms) by the current language
    2. when the lang key is set and has a value (like 'de,fr') Polylang filters the languages according to that value
    [ so far nothing new, but then ]
    3. when the lang key is set but has no value (like '') Polylang does not filter by language(s)

    Similar to some other parameter key / value pairs work in WordPress query functions.

    You see, while adding a 'lang' key / value is without risk (because it will simply be ignored when Polylang is not installed) but the foreach loop to check for configured languages will have to be wrapped in an if statement to make sure the $polylang object is set and even then it might break some day (in the far future) if you ever decide to somehow alter how languages are stored in the polylang object affecting how get_languages_list() returns.

    Well, a lot of ifs and I trust you are keen to keep the objects public functions consistent so it's just a suggestion :)

  7. RavanH
    Member
    Posted 1 year ago #

    Hi Chouby,

    I notice that this does not work anymore in later versions than 0.9.5.3 ... Was the 'lang' array value for get_terms() dropped? I'd really love to have this back again :)

  8. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    For sure not! But I introduced a bug when refactoring things to support the same for comments. It should work now in the current development version (1.0.1.6)
    http://downloads.wordpress.org/plugin/polylang.zip

  9. RavanH
    Member
    Posted 1 year ago #

    Yep, that works again! Thanks :)

    Any chance you could consider my earlier suggestion?

    1. when the lang key is not set, Polylang filters posts (or terms) by the current language
    2. when the lang key is set and has a value (like 'de,fr') Polylang filters the languages according to that value
    [ so far nothing new, but then ]
    3. when the lang key is set but has no value (like '') Polylang does not filter by language(s)

    Similar to how other parameter key / value pairs work in WordPress query functions.

    ;)

  10. RavanH
    Member
    Posted 1 year ago #

    There is a problem with the development version:

    When NOT having configured any menus but simply relying on the default WordPress generated one, on a Twenty Eleven site, I notice the menu items are no longer filtered by language...

  11. RavanH
    Member
    Posted 1 year ago #

    Oh, and the admin section language filter (in the top nav) does not seem to work anymore...

  12. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Hi!

    I reproduced the bug with the default menu (in fact, it affects all lists of pages). I will update the development version soon.

    But I don't have any issue with the admin language filter.

    Regarding your suggestion (3), I will try to look at this in the future (I write that in my todo list), but don't expect this soon. You know...

  13. RavanH
    Member
    Posted 1 year ago #

    Regarding your suggestion (3), I will try to look at this in the future (I write that in my todo list), but don't expect this soon. You know...

    I know, and I completely understand. For now, my xml sitemap plugin uses a dedicated polylang get languages routine :)

    Regarding the admin language filter, I found that I was clicking the radio buttons and not the text. When you do that, you'll notice that you can activate all radios at the same time but nothing happens... Only when you click the text, the page refreshes and comes back with the filtered posts. Not a big issue but still, it confused me a bit ;)

  14. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Strange. Can you reproduce in other browsers?

  15. RavanH
    Member
    Posted 1 year ago #

    You're right, it seems to be only happening in Chrome/Chromium but not Firefox or Opera. Did not test others...

  16. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    So I fear I can't do nothing to help... Basically, I pass html to WordPress functions. Nothing special...

  17. RavanH
    Member
    Posted 1 year ago #

    OK, then it's probably just because the use of the form input radio there is not really correct html... ;)

    It might be more corss-browser compatible if there is an actual form that does a submit on change. Like this:

    ...
    <form class="ab-sub-wrapper" target="#" method="get">
      <ul id="wp-admin-bar-languages-default" class="ab-submenu">
        <li id="wp-admin-bar-all">
          <label><a class="ab-item"  href="http://status301.net/wp-admin/?lang=all">
            <input name="lang" type="radio" value="all" onchange="this.form.submit();"  />
            Show all languages</a></label></li>
        <li id="wp-admin-bar-en">
          <label><a class="ab-item"  href="http://status301.net/wp-admin/?lang=en">
            <input name="lang" type="radio" value="en" onchange="this.form.submit();"  />
            English</a></label></li>
        <li id="wp-admin-bar-nl">
          <label><a class="ab-item"  href="http://status301.net/wp-admin/?lang=nl">
            <input name="lang" type="radio" value="nl" onchange="this.form.submit();"  />
            Nederlands</a></label></li>
        <li id="wp-admin-bar-fr">
          <label><a class="ab-item"  href="http://status301.net/wp-admin/?lang=fr">
            <input name="lang" type="radio" value="fr" onchange="this.form.submit();" checked="checked" />
            Fran├žais</a></label></li>
      </ul>
    </form>
    ...

    Notice the wrapper div is now a form with GET method and the input radio tags use their name (should all have the same) and value attributes differently... This way, both a link click as an input field change should result in the same thing :)

  18. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    For sure not! But I introduced a bug when refactoring things to support the same for comments. It should work now in the current development version (1.0.1.6)
    http://downloads.wordpress.org/plugin/polylang.zip

    A new bug was detected and so I had to rework this code once again. The current development version is updated with a new correction if you want to test it.

    OK, then it's probably just because the use of the form input radio there is not really correct html...

    Hopefully, the fact that I use input radio outside a form is not the issue (I have read somewhere that it is correct) as it would be very hard if not impossible to include the form tag in the WP admin bar. However I corrected the bad usage of name and value (where was my brain when I wrote this ???) in the current development version. Maybe it will correct your issue as I still don't understand how it could work in other browsers...

  19. RavanH
    Member
    Posted 1 year ago #

    A new bug was detected and so I had to rework this code once again. The current development version is updated with a new correction if you want to test it.

    Thanks! I'll be testing it on my dev install. Will let you know if I run into anything :)

    So far, I notice that the radio buttons now work correctly as in "they switch off when another one is selected" but clicking them does not result in the page refreshing with the ?lang= request variable.

    It's probably a stacking order in Chrome where the button (partially) covers the link itself. What if you add an onclick event redirecting to the correct URL? Is that possible in admin bar entries?

  20. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Yes. I will include this in the next version.

  21. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    3. when the lang key is set but has no value (like '') Polylang does not filter by language(s)

    Since it was quite easy to do, this will be included in 1.0.3

  22. RavanH
    Member
    Posted 1 year ago #

    Fantastic!

    Question: I ran into a little issue when wanting to list all categories in all languages (which I'm doing so far with that 'lang' => implode(',', $langs), parameter)... I noticed that if you have a taxonomy term that has not been assigned to any language (yet) then that term will not be listed. I'm hoping the 'lang' => '', will now include unassigned taxonomy terms as well?

  23. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Yes it should do what you are expecting. When using 'lang' => '', Polylang does not apply any filter.

  24. RavanH
    Member
    Posted 1 year ago #

    Just perfect :) I'll be adapting my next version of XML Sitemap Feeds...

  25. Engr.MTH
    Member
    Posted 11 months ago #

    Hi Chouby
    i tried to get posts in all languages but i couldn't ,i tried this code you gave

    $posts = get_posts(array(
    'post_type' => 'post',
    'lang' => 'de,fr', // query German and French posts
    'showposts' => 5
    ));
    i putted in the post.php in wordpress but it didn't work out can you plz give me more explanation about how and where to put the code
    p.s i'm realy new to wordpress

  26. RavanH
    Member
    Posted 11 months ago #

    Hi Engr.MTH, please do not edit WordPress core files. Your modifications can ruin your installation or at least will be overwritten / erased up on the next update. Use code like the example given above in theme template files. To learn about theme modifications, start with http://codex.wordpress.org/Child_Themes :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic