Multisite Network with Polylang Plugin

What happens when you add a new language?

Let’s say that we’ve added russian language in Polylang menu. What happens behind the scenes? How this language is stored in database?

Polylang usage within a Multisite Network - add new language

Each language is a taxonomy. Taxonomy with show_ui=false. When you add languages on different websites of your multisite network, taxonomies will be created separately for each website in wp_{blog id}_terms tables.

Each translation is a post that is in a specific language taxonomy.

How to make Polylang and Network_Query to work together?

Now we already know that translations are posts, languages are taxonomies. Once you rebuild the index, all posts and their translations will go to wp_network_posts, languages will go to wp_network_terms. If these tables are empty and rebuilding the index doesn’t help, just reinstall the plugin.

By default Network_Query will return all posts from all languages. To get the current language posts you have to pass language term slug in tax_query parameter.


$current_lang_slug = pll_current_language(); // current language slug

$multisite_query = new Network_Query( array(
	'tax_query' => array(
		array(
			'taxonomy' => 'language',
			'field' => 'slug', // should be "slug"
			'terms' => $current_lang,
		)
	),
	'post_type' => 'post',
	'post_status' => 'publish'
));

What about terms? Is there a chance to get terms by a specific language?

Yes, it is. In 4.8.1 version I added network_get_translated_terms() that is very similar to network_get_terms(), except one parameter:


network_get_translated_terms( $taxonomies, $args = '', $languages )
languages
(array|string) Pass your language slug or an array of slugs here, example: array( 'en', 'fr').

If you have suggestions for my new posts, please leave it in comments below 😺

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter