Pods integration with Polylang
-
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.
-
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.
Great, thanks. Will get this integrated w/ soon, should be Pods 2.3:
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?
Also, what function/constant/var should I check to see if Polylang is translating a specific post type / taxonomy?
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.
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’
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"
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' ) )
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"
For reference, here’s what I’ve coded up for WPML support:
Post Types – https://sc0tt.me/image/3E2b2s1H1k1J
Taxonomies – https://sc0tt.me/image/2r2F252Y2e1J
Code is in now, once I know the correct joins to use, I’ll modify it to incude official Polylang support ??
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.
https://downloads.www.ads-software.com/plugin/polylang.zipGreat, 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:
Added into Pods 2.3!
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.
- The topic ‘Pods integration with Polylang’ is closed to new replies.