If you are using WPML, Polylang or Polylang Pro, download and install the Multilingual add-on to integrate your translations with FacetWP.

WPML

The Multilingual add-on supports normal translations, as well as dynamic strings (e.g. facet placeholder text) via the WPML String Translation plugin.

The add-on supports sub-site (fr.mysite.com) and sub-directory (mysite.com/fr/) permalinks, but does not support URL strings (?lang=fr).

WPML translation setup

In order for WPML to work well with FacetWP, you need to correctly set up the Post Types Translations and Taxonomies Translations in WPML’s settings. Go to:

WPML > Settings > Multilingual Content Setup

Then scroll down to the “Post Types Translation” and “Taxonomies Translations” tables.

For each post type and taxonomy that you are using in post listings with facets, make sure you select the first option: “only show translated items”:

WPML Post Types and Taxonomies Translation setting - Select Only show translated items. Do not use the second (fallback) option!
WPML Post Types and Taxonomies Translation setting – Select “Only show translated items”. Do not use the second (fallback) option!

Then, in WPML > Translation Management and WPML > Taxonomy translation, make sure every item has a translation. Or you can use the bulk edit feature to create duplicates for all items, for as long as the content is not translated yet.

The second option, “use translation if available or fallback to default language”, does not work well with FacetWP. If you select that option and you have posts without a translation, you will have the fallback (original language) posts in the translated language results, and those posts will have their (original language) categories/terms indexed. This will result in duplicate categories/terms showing up in facets: in the translated language and in the original language.

Keep in mind that when FacetWP indexes a post, it determines that post’s language and indexes any related content for that language. FacetWP does not support the concept of a single post belonging to multiple languages, so you essentially need to have a translated post for each language. If a translation is not available, you need to duplicate the post in the original language.

If you are using (ACF) custom fields as data source for your facet, make sure that those fields are set to be translatable, and are translated for each post in each language.

Solving issues with WPML

If you encounter any issues with WPML, like posts, terms or other data not appearing, make sure your WordPress install has enough memory. WPML’s minimum requirements are 128M, and 256M is recommended.

In our performance tuturial you can read how to give WordPress more memory.

You can see how much memory is currently set by going to WPML > Support. In the WordPress section, you’ll see what is set as WordPress’ memory limit. This has to be lower or equal to what is available on the server, which you can see in the PHP section under “Memory limit”.

If you go to WPML > Support > Troubleshooting, you’ll see a lot of buttons with which you can clean up WPML’s cache and database. Make sure you have a database backup before using that.

Polylang

The Multilingual add-on supports normal Polylang translations.

Make sure to go through all configuration steps of the setup wizard, which is automatically launched when activating Polylang or Polylang Pro for the first time. In this process, one or more languages are assigned to your content.

If you skip(ped) this step, then your existing content does not have a language set, which causes posts to not show up in your template’s query. You can fix this by running the wizard again, or assigning a language in bulk.

Don’t use Polylang Pro’s shared slug feature

Polylang Pro comes with a feature that makes it possible for posts, pages or terms to share the same slug across translations.

Currently, this feature does not work with FacetWP. With shared slugs, on pages with the non-default language facets will not return expected options.

The solution for now is to use unique slugs across translations.

Using WPML or Polylang with SearchWP

If you are using SearchWP on a multilingual site with WPML or Polylang, make sure to install the appropriate SearchWP integration extension:

These extensions will limit search results to the active language of the page. Without them, there will be a disconnect between the facets, their counts and the post listing.

Translate UI strings and facet labels with the facetwp_i18n filter hook

Generally, text strings that appear in the UI of your site and in FacetWP will be translatable using __() or _e(). You’d use your translation plugin, or a WordPress gettext filter (see this example).

However, some of the UI strings that appear in various facet types are dynamic, “in-database” strings, which means that WPML or Polylang cannot see them. Fortunately, there is a way to translate these strings: with the facetwp_i18n filter hook.

This filter hook can also be used to translate facet labels. Note that facet labels are something different than facet names. Facet labels appear as facet heading in the Mobile Flyout and as label in the User Selections facet).

Translate facet choices

There are multiple ways to translate facet choices. The obvious way is to translate the content of the source field that the facet uses. For example, if your facet uses a taxonomy as data source, you can translate the taxonomy terms with Polylang or WPML.

Alternatively, you can translate facet choices manually. This can be done with the facetwp_facet_display_value hook, or with the facetwp_facet_render_args hook.

Fix indexing errors with Polylang or WPML installed

When you have Polylang or WPML installed without the Multilingual add-on, you can get a “The index table is empty” error when trying to re-index. To fix this, simply install the add-on and re-index.

Fix issues with filtering posts with no language set

WPML and Polylang automatically query only posts for the current language. When the Multilingual add-on is active, FacetWP also expects posts to have a language. It gets the current language from the page and stores it in the FWP_HTTP['lang'] parameter in order to remember it during facet refresh, and filter only the posts in this language.

There are some scenarios in which this language parameter causes unexpected filtering issues. You may for example have a listing template that queries a custom post type with no language set for its posts, on which you are effectively not using WPML or Polylang. Or you may have an archive in which you want to retrieve and filter posts of all languages. In these situations you need to prevent FacetWP from using the current language when filtering.

To remove the language parameter from FacetWP’s AJAX refresh, add the following snippet to your (child) theme’s functions.php:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_action( 'facetwp_scripts', function() { // Use the correct conditional for your template(s), e.g. is_archive(), is_page(), is_category() etc. // See: https://codex.wordpress.org/Conditional_Tags if ( is_post_type_archive( 'my_custom_post_type' ) ) { ?> <script> document.addEventListener('facetwp-refresh', function() { delete FWP_HTTP['lang']; }); </script> <?php } }, 100 );

Note that without a conditional tag, this snippet will remove the language parameter from all pages and templates. If you have pages, templates, or archives on which you do want to filter posts of a specific language, you need the language parameter in place. So make sure to add the correct conditional tag to the above snippet in line 2, so it only works on the page or archive it is intended for.

Changelog

1.0.1

  • Fixed minor code cleanup / modernization

1.0.0

  • Fixed the correct element_type was not being passed into WPML (props Bram)

0.2.2

  • Fixed reset WPML language after indexing
  • Fixed make sure Polylang language variable is defined

0.2.1

  • Fixed WPML class name (props Derrick Hammer)

See also