WordPress.org

Ready to get started?Download WordPress

Forums

Polylang
[resolved] Pods integration with Polylang (21 posts)

  1. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Looking for information about how Polylang stores it's DB info per post / taxonomy / etc.

    I need to do custom JOINs for the queries Pods makes, and pass those JOINs which current language is set.

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

  2. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Polylang defines a taxonomy 'language'.

    Each language is stored as a term in that taxonomy
    * name -> language name
    * slug -> language code
    * description -> WordPress locale
    * term_group -> order (in which the languges are displayed in the widget).

    So for posts, I use the normal way of working with taxonomies. see the function set_post_language in include/base.php.

    For terms, I created a new table called $wpdb->prefix . 'termmeta'. I use the standard WP meta API to communicate with this table. And the language's term_id is stored with the meta key '_language'. See the function set_term_language in include/base.php.

    Translations are stored either as a postmeta or a termmeta with the key '_translations'. The value is an array like: array('en' => 1, 'fr' => 2). The same array is associated to all translations.

    Don't hesitate to ask if you have other questions.

  3. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Great, thanks. Will get this integrated w/ soon, should be Pods 2.3:

    https://github.com/pods-framework/pods/issues/974

  4. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Can you provide a little more information so I can knock this out for Pods 2.3?

    • What's the function / variable / constant to call to get the current language?
    • Can you provide the table breakdown for what field are in wp_termmeta?
      • How does Polylang handle terms that use the same slug, but are in different languages and/or taxonomies? Or is termmeta storing wp_term_taxonomy ids too?
  5. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Also, what function/constant/var should I check to see if Polylang is translating a specific post type / taxonomy?

  6. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    To get the current language (en, es, and others), I can use pll_current_language( 'slug' )

    I looked through the codebase and was unable to figure out the proper function to check if a post type or taxonomy is being translated by Polylang. In WPML, I can call that like this for a Post Type: $sitepress->is_translated_post_type( $post_type )

    I'm hoping there's something I can utilize in Polylang for that type of check, since the post_types and taxonomies variables are marked private in the Polylang_Base class.

  7. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Can you provide the table breakdown for what field are in wp_termmeta

    The fields are the same as all other meta tables in WordPress. The keys used are:
    * _rtl (value 0 or 1) used for languages only to know if they are ltr or rtl
    * _language (value equal to the language term_id)
    * _translations (the value is an array with language slugs as keys and translated term_id as values)

    How does Polylang handle terms that use the same slug, but are in different languages and/or taxonomies? Or is termmeta storing wp_term_taxonomy ids too

    * If you attempt to create a new term with the same slug in a different language then you will get an error message.
    * If you don't precise the slug and attempt to create a new term with the same name in a different language then Polylang will create the default slug and appends something like '-en'.
    If you attempt to create a new term with the same name in a dif* ferent taxonomy, you will have the same comportement as without Polylang (shared terms).
    * Since 1.0, Polylang avoids sharing the same term between a 'language' and any other taxonomy.

    I'm hoping there's something I can utilize in Polylang for that type of check, since the post_types and taxonomies variables are marked private in the Polylang_Base class

    There is clearly a lack here. Consider that the next version (1.0.1, which will probably released very soon) will provide two new functions 'pll_is_translated_post_type' and 'pll_is_translated_taxonomy'

  8. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Thanks for pll_is_translated_post_type / pll_is_translated_taxonomy, that will really help!

    As for joins, I think I'm going to need to get a precise SQL query example from you as I'm still not sure how best to handle this on my own right now.

    Please amend this query with how to limit to only terms in the 'en' language:

    SELECT t.*
    FROM wp_terms AS t
    LEFT JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id
    WHERE tt.taxonomy = "my_taxonomy"
  9. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    I'm coding up the pll_is_translated_post_type code right now, will the following code work for the way you're going to build it?

    if ( pll_is_translated_post_type( 'my_post_type' ) )

    and

    if ( pll_is_translated_taxonomy( 'my_taxonomy' ) )

  10. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Also, please provide the amended query for this to limit only posts in the 'en' language:

    SELECT t.*
    FROM wp_posts AS t
    WHERE t.post_type = "my_post_type"
  11. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    For reference, here's what I've coded up for WPML support:

    Post Types - http://sc0tt.me/image/3E2b2s1H1k1J

    Taxonomies - http://sc0tt.me/image/2r2F252Y2e1J

  12. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Code is in now, once I know the correct joins to use, I'll modify it to incude official Polylang support :)

  13. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Please amend this query with how to limit to only terms in the 'en' language:

    First you should get the language term_id:

    global $polylang;
    $lang_id = $polylang->get_language('en')->term_id;

    then the query should be:

    SELECT t.*
    FROM wp_terms AS t
    LEFT JOIN wp_term_taxonomy AS tt ON tt.term_id = t.term_id
    LEFT JOIN $wpdb->termmeta AS pll_tm ON t.term_id = pll_tm.term_id
    WHERE tt.taxonomy = "my_taxonomy"
    AND pll_tm.meta_key = '_language' AND pll_tm.meta_value = $lang_id

    you can also use:

    $terms = get_term("my_taxonomy", array('lang' => 'en');

    Also, please provide the amended query for this to limit only posts in the 'en' language

    First you should get the language term_taxonomy_id:

    global $polylang;
    $lang_tt_id = $polylang->get_language('en')->term_taxonomy_id;

    then the query should be:

    SELECT p.*
    FROM wp_posts AS p
    INNER JOIN $wpdb->term_relationships AS tr on tr.object_id = p.ID
    WHERE p.post_type = "my_post_type" AND tr.term_taxonomy_id = $lang_tt_id;

    not tested as I prefer to use:

    $posts = get_posts(array('post_type' => "my_post_type", 'lang' => 'en');

    Your usage of pll_is_translated_post_type and pll_is_translated_taxonomy is correct.

    I just uploaded a dev version (1.0.0.1) for you to be able to make tests.
    http://downloads.wordpress.org/plugin/polylang.zip

  14. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Great, didn't see this before, will implement right away!

    Also, latest release has some issues if you don't have any post types / taxonomies selected to be translated:

    http://sc0tt.me/image/003Z470t1W0o

  15. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Added into Pods 2.3!

  16. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    Great!

    'post', 'page', 'category', 'post_tag' should always be translated. I should have precised that pll_is_translated_post_type and pll_is_translated_taxonomy do not work before the action 'wp_loaded' is fired. Do you call the function in a 'init' hook?

    But at least, I have to make my code more robust to avoid this error.

  17. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Yeah, that's probably what it is. It runs before your hooks run. Is there another way to check?

  18. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Also, have you considered using GitHub for your code so people like me can help out where they're able? I'd love to help out, there are a number of features I'm very keen on getting into Polylang.

  19. Chouby
    Member
    Plugin Author

    Posted 1 year ago #

    The problem is to get an accurate result. I expect that plugins will create post types in an 'init' action and it is likely (although not mandatory) that they declare the post type as translatable at the same time. It's for example what I currently do when reading the file wpml-config.xml. I examined how it is done in WPML 2.0.4.1, and for sure the list is not accurate before ('init', 15) as been fired.

    That's the reason why I wait for 'wp_loaded'. At which time in the WP loading process do you need the list?

    Also, have you considered using GitHub for your code so people like me can help out where they're able?

    Not until now as I don't even know what I could do / not do with it. What kind of features would you like to add to Polylang?

  20. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    I'm going to do another audit to see how we can improve the Pods action flow to get around this.

  21. Scott Kingsley Clark
    Member
    Posted 1 year ago #

    Following up, everything should be good in Pods 2.3 on this. I've got a workaround that comes back around and fills in the Polylang related stuff after Polylang has been made available.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags

No tags yet.