Categories
Taxonomies WMPL Wordpress

[WMPL Fix]: hide_empty => true not working in get_terms()

My terms are showing up (using get_terms() function), even if they are not supposed to show because they don’t have any published posts in my selected language

Hello friends!

Just now I encountered an issue that my terms are showing up (using get_terms() function), even if they are not supposed to show because they don’t have any published posts in my selected language.

I have WPML installed on my website and at the time of writing, I’m still in the process of setting up the styles, theme configuration, and other stuff.

The Problem

Just to elaborate further on what I’m talking about, the following terms heritage_location should show empty posts in the Arabic language because I haven’t translated anything yet on any posts associated with them.

The highlighted column shows supposed to be all zeros (0).

Terms list in the backend

The following code returns also the terms even if they are empty.

$rows = get_terms( array(
		'taxonomy' => 'heritage_location',
		'hide_empty' => true,
	) );

The Solution

After a lot of head scratches, I just found out what I missed doing.

When WPML is installed and activated, custom taxonomies and post types are not translatable by default.

To make them translatable, from the left menu panel, head over to WMPL > Settings, scroll down the page until you reach the Taxonomies Translation table.

WPML Settings screenshot in the backend

By default, Categories (category) and Tags (post_tag) taxonomies, which are built-in in WordPress are translatable.

Find all the custom taxonomies such as those you’ve created within your theme or from the CPT UI plugin, even the ones created by other plugins, and mark them translatable by clicking the radio button in the Translatable column. Then click the Save button located at the end of the table.

WPML Settings screenshot in the backend

NOTE: There are taxonomies that might be disabled. To enable them, click the lock icon beside each taxonomy title.

When you go back to your terms list in the backend, only those terms that have published posts in your selected language will be listed. When the taxonomy is translatable, you should also see the language filter on top of the table.

The same applies when you use get_terms() function with the argument hide_empty=>true.

Terms list in the backend with the language filter

For custom post types, you can do the same procedure. Find the Post Types Translation on the WPML settings page.

That’s it folks, let me know in the comment below if this article has been helpful to you.

God bless everyone!