Multisite Network with Polylang Plugin

In this small tutorial I will show how Polylang plugin works in WordPress multisite network and give you some tips how to use it together with my multisite 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 😺

Related Posts

Misha Rudrastyh

Misha Rudrastyh

I love WordPress, WooCommerce and Gutenberg so much. 10 yrs of experience.

Need some custom developer help? Let me know

Follow Misha

Need some help with WordPress Multisite?

If you need some professional developer help, I will be happy to assist you.

Contact me Who I am?

Leave a comment

php js HTML CSS Code