Skip Navigation

Open

Overview of the issue

The native WordPress function has_category does not work for translated posts.

An example of this is as follows:

  1. We have a category with the English slug “test” and its French translation “test-fr”.
  2. This category is assigned to posts.
  3. When you invoke the has_category(“test”) inside a loop in your theme’s single.php file, it returns true for the default language but false for the translation.

Workaround

Define a new filter in your theme’s functions.php file:

function wp_has_category ($category) {
$category = get_term_by('name', $category, 'category');
return has_category($category);
}
add_filter('wp_has_category', 'wp_has_category', 10, 1);

Replace the has_category function with this new filter:

apply_filters( 'wp_has_category', 'test' )

This filter takes the translated category based on the name of original one and checks if the translated category is assigned to a post.

4 Responses to “The has_category function does not work for translated posts”

  1. The WPML plugin is duplicating my website title on Search Engines…
    This is really bad for SEO, can someone help me, please?????

Leave a Reply

Please stay on topic and be respectful to others. If you need help with issues not related to this post, use our Support Forum to start a chat or submit a ticket.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>