Hide Untranslated Posts in WordPress for Better User Experience

How to hide untranslated posts in WordPress; Exclude non-translated posts from WordPress site; Hide posts without translation in WordPress; Prevent untranslated posts from showing in WordPress; WordPress hide posts not translated; Remove untranslated posts from WordPress category page; WordPress exclude posts without translation; Hide non-translated content in WordPress; WordPress do not display untranslated posts; How 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

<?php
// Function to exclude untranslated posts from queries
function wp_dudecom_exclude_untranslated_posts( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        // Check if a translation plugin is active
        if ( function_exists( 'pll_current_language' ) ) {
            $current_language = pll_current_language();
            $query->set( 'lang', $current_language );
        } elseif ( function_exists( 'icl_object_id' ) ) {
            global $sitepress;
            $current_language = $sitepress->get_current_language();
            $query->set( 'lang', $current_language );
        }
    }
}
add_action( 'pre_get_posts', 'wp_dudecom_exclude_untranslated_posts' );

// Function to ensure untranslated posts are not displayed in category pages
function wp_dudecom_exclude_untranslated_posts_in_category( $query ) {
    if ( ! is_admin() && $query->is_category() && $query->is_main_query() ) {
        // Check if a translation plugin is active
        if ( function_exists( 'pll_current_language' ) ) {
            $current_language = pll_current_language();
            $query->set( 'lang', $current_language );
        } elseif ( function_exists( 'icl_object_id' ) ) {
            global $sitepress;
            $current_language = $sitepress->get_current_language();
            $query->set( 'lang', $current_language );
        }
    }
}
add_action( 'pre_get_posts', 'wp_dudecom_exclude_untranslated_posts_in_category' );
?>

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.