Snippet

Hide Untranslated Posts in WordPress for Better User Experience

How to hide untranslated posts in WordPressExclude non-translated posts from WordPress siteHide posts without translation in WordPressPrevent untranslated posts from showing in WordPressWordPress hide posts not translatedRemove untranslated posts from WordPress category pageWordPress exclude posts without translationHide non-translated content in WordPressWordPress do not display untranslated postsHow to exclude posts not translated in WordPress

Explanation

If you want to hide posts that haven't been translated into the language your visitors are viewing, this code is your friend. It works by checking which language is currently active on your site and then only showing posts that have been translated into that language.

How It Works:

  • The code hooks into WordPress's query system, which is responsible for fetching posts to display.
  • It checks if you're using a translation plugin like Polylang or WPML.
  • If Polylang is active, it uses the current language setting from Polylang.
  • If WPML is active, it uses the current language setting from WPML.
  • It then adjusts the query to only fetch posts available in the current language.

Where It Applies:

  • On the main site pages, ensuring untranslated posts are hidden.
  • Specifically on category pages, so visitors only see posts in their selected language.

This setup ensures your visitors only see content that's been translated, providing a cleaner and more relevant browsing experience.

Code

1<?php 2// Function to exclude untranslated posts from queries 3function wp_dudecom_exclude_untranslated_posts( $query ) { 4 if ( ! is_admin() && $query->is_main_query() ) { 5 // Check if a translation plugin is active 6 if ( function_exists( 'pll_current_language' ) ) { 7 $current_language = pll_current_language(); 8 $query->set( 'lang', $current_language ); 9 } elseif ( function_exists( 'icl_object_id' ) ) { 10 global $sitepress; 11 $current_language = $sitepress->get_current_language(); 12 $query->set( 'lang', $current_language ); 13 } 14 } 15} 16add_action( 'pre_get_posts', 'wp_dudecom_exclude_untranslated_posts' ); 17 18// Function to ensure untranslated posts are not displayed in category pages 19function wp_dudecom_exclude_untranslated_posts_in_category( $query ) { 20 if ( ! is_admin() && $query->is_category() && $query->is_main_query() ) { 21 // Check if a translation plugin is active 22 if ( function_exists( 'pll_current_language' ) ) { 23 $current_language = pll_current_language(); 24 $query->set( 'lang', $current_language ); 25 } elseif ( function_exists( 'icl_object_id' ) ) { 26 global $sitepress; 27 $current_language = $sitepress->get_current_language(); 28 $query->set( 'lang', $current_language ); 29 } 30 } 31} 32add_action( 'pre_get_posts', 'wp_dudecom_exclude_untranslated_posts_in_category' ); 33?>

Instructions

File Location: Add the code to your theme's functions.php file or a custom plugin file.

Prerequisites:

  • Ensure you have a translation plugin installed and activated, such as Polylang or WPML.

Implementation Steps:

  1. Access Your WordPress Dashboard: Log in to your WordPress admin panel.
  2. Navigate to Theme Editor: Go to Appearance > Theme Editor. If you prefer using a custom plugin, navigate to Plugins > Add New and create a new plugin.
  3. Open functions.php: In the Theme Editor, locate and open the functions.php file from the list on the right. If using a custom plugin, open the plugin file.
  4. Insert the Code: Copy the provided code and paste it at the end of the functions.php file or your custom plugin file.
  5. Save Changes: Click the Update File button to save your changes.
  6. Test Your Site: Visit your site and navigate to different pages and categories to ensure untranslated posts are hidden in the selected language.

If you encounter any issues or need further assistance with this implementation or more advanced functionality, consider reaching out to wp-dude.com for expert help.