WordPress Polylang Filter nicht übersetzte Artikel anzeigen!

How to Filter Untranslated Posts in WordPress using Polylang

Table of Contents

How to Filter Posts without Translation in the WordPress Admin

In this article, I’ll show you how to filter posts in the WordPress admin area to display only those that do not have a translation. This guide will walk you through the steps to add a dropdown menu allowing you to choose between all posts and those without translation.

Step 1: Adding the Dropdown Menu

Open the functions.php file of your WordPress theme and add the following code at the end:

				
					function add_filter_dropdown() {
    global $pagenow, $typenow;

    // Only display on the posts page in the admin area
    if ($pagenow == 'edit.php' && $typenow == 'post') {
        ?>
        <select name="translation_filter" id="translation_filter">
            <option value="all" <?php selected(isset($_GET['translation_filter']) && $_GET['translation_filter'] == 'all'); ?>><?php _e('All Posts', 'textdomain'); ?></option>
            <option value="no_translation" <?php selected(isset($_GET['translation_filter']) && $_GET['translation_filter'] == 'no_translation'); ?>><?php _e('Posts without Translation', 'textdomain'); ?></option>
        </select>
        <script>
            // JavaScript to monitor the dropdown menu selection and reload the page when the selection changes
            document.getElementById('translation_filter').addEventListener('change', function() {
                var value = this.value;
                var url = new URL(window.location.href);
                if (value === 'no_translation') {
                    url.searchParams.set('translation_filter', 'no_translation');
                } else {
                    url.searchParams.delete('translation_filter');
                }
                window.location.href = url.toString();
            });
        </script>
        <?php
    }
}
add_action('restrict_manage_posts', 'add_filter_dropdown');

				
			

Step 2: Filtering Posts without Translation

Also, add the following code to your functions.php file to filter posts without translation:

				
					function filter_posts_without_translation($query) {
    global $pagenow, $wpdb;

    // Only in the admin area and on the "Posts" page, and if the filter is activated
    if (is_admin() && $pagenow == 'edit.php' && $query->is_main_query() && isset($_GET['translation_filter']) && $_GET['translation_filter'] == 'no_translation') {
        $sql = "
            SELECT p.*
            FROM wp_posts p
            LEFT JOIN (
                SELECT object_id
                FROM wp_term_relationships
                WHERE term_taxonomy_id IN (
                    SELECT term_taxonomy_id
                    FROM wp_term_taxonomy
                    WHERE taxonomy = 'post_translations'
                )
            ) AS translated_posts ON p.ID = translated_posts.object_id
            WHERE p.post_type = 'post'
            AND p.post_status = 'publish'
            AND translated_posts.object_id IS NULL
            ORDER BY p.ID DESC;
        ";

        // Execute the SQL query
        $post_ids = $wpdb->get_col($sql);

        // Apply filter to display only the articles returned in the SQL query
        $query->set('post__in', $post_ids);
    }
}
add_action('pre_get_posts', 'filter_posts_without_translation');

				
			

Save your functions.php file and refresh the posts page in the WordPress admin area. You should now see a dropdown menu allowing you to choose between all posts and those without translation.

That’s it! You have successfully learned how to filter posts in the WordPress admin area to display only those without translation.

*This article contains affiliate links. By clicking on them, you will be directed to the provider. If you decide to make a purchase there, I will receive a small commission. Thank you for your support! Offers are only valid at the time of publication of the article. Prices may change. Please check the current price on the linked website.

Broschi
Broschi
Hi, I'm Andy. I've been creating websites since 1999, so I'm perfectly versed in WordPress, PHP, affiliate marketing and especially SEO. I love technology, especially PCs with everything that goes with it!
Amazon Shop
Affiliate Link!
ebay Angebote
Affiliate Link
Broschis Blog Newsletter
NEWSLETTER ABONNIEREN!
Share the excitement: Tell your friends about this article! And let us hear your thoughts - leave your comment now!

Your thoughts are wanted! Do you have any questions or ideas for the article? Let them out. Your comment opens the door to lively discussions and provides a platform for sharing knowledge and help among readers. Feel free to also share if you felt something was missing – we’re eager to hear your opinion!

Please enter your comment below:

Your email address will not be published. Required fields are marked *