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') {
?>
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.