,

How to Stop Polylang from Duplicating Attachments

How to Stop Polylang from Duplicating Attachments

If your Media Library is filling up with “translated” copies of PDFs, Word files, spreadsheets, and other documents, you’re seeing Polylang’s automatic media duplication at work. That’s handy for images (different alt text, captions, or featured images per language), but it’s usually noise for documents that don’t change across languages. Polylang creates extra attachment posts — not extra files on disk — which clutters the library and slows down editors. Polylang

The real-world problem we hit

On a multilingual WordPress site, the German search results were showing Dutch PDFs. In wp-admin it looked like those Dutch files “weren’t there,” but in Polylang’s Media list view, every document showed a pencil icon under all language columns (and a “+” only where a translation didn’t exist yet). That means each upload had been duplicated into every language as separate attachment posts.

Problem Confirmation

To confirm, we ran this SQL (replace the slug with yours):

SELECT p.ID,
       t.slug  AS lang_code,
       t.name  AS lang_name
FROM   wp_posts p
JOIN   wp_term_relationships tr ON tr.object_id = p.ID
JOIN   wp_term_taxonomy tt ON tt.term_taxonomy_id = tr.term_taxonomy_id
AND tt.taxonomy = 'language'
JOIN  wp_terms  t  ON t.term_id = tt.term_id
WHERE  p.post_type = 'attachment'
AND  p.post_name = 'your-document-name'
ORDER  BY t.slug;

Result: one attachment row per language for the same document slug. Search was “right,” but the content model wasn’t what we wanted.

Automatically duplicate media in all languages when uploading a new file in Polylang

Why this happens: With Languages → Settings → Media → “Automatically duplicate media in all languages when uploading a new file” enabled, Polylang creates translated attachment posts for each language (the physical file on disk is not duplicated). Polylang’s guide “Working with media” explains this behavior: https://polylang.pro/doc/working-with-media/

Our Goal Would Be:

  • Do duplicate images (alt text, captions, featured images can vary by language).
  • Don’t duplicate documents (PDF/DOC/XLS/PPT/CSV/TXT/etc.), which just create clutter and cross-language search noise.

The clean fix: allow only image/* to duplicate

Polylang provides the pll_enable_duplicate_media filter that fires for every upload and decides whether to create translated attachments.

Install this as a tiny MU plugin (wp-content/mu-plugins/polylang-media-control.php). MU plugins are perfect for site-wide policy: https://developer.wordpress.org/plugins/mu-plugins/

So here is our custom mu-plugin. You can also just add this code to your website’s custom plugin.

<?php
/**
 * Plugin Name: Polylang – Only duplicate images
 * Description: Prevent Polylang from duplicating non-image attachments (PDF, DOCX, XLSX, PPTX, CSV, TXT, etc.).
 */

// Respect Polylang's own decision and allow duplication only for images.
add_filter( 'pll_enable_duplicate_media', function ( $enable, $post_id ) {
    // WP core helper: https://developer.wordpress.org/reference/functions/get_post_mime_type/
    $mime = get_post_mime_type( $post_id );
    if ( ! is_string( $mime ) ) {
        return $enable;
    }

    // PHP 8+: return $enable && str_starts_with( $mime, 'image/' );
    return $enable && ( 0 === strpos( $mime, 'image/' ) );
}, 10, 2 );

Why this works (and passes code review):

What you’ll see in wp-admin after this change

  • Images: pencil icons under other languages (translations exist).
  • Documents: “+” under other languages (no translations created).

How to totally disable media duplication (images included)

If you want one global Media Library with zero per-language attachments:

Option A — UI (quickest):
Go to Languages → Settings → Media and uncheck “Automatically duplicate media in all languages when uploading a new file.”

Option B — Code (enforce site-wide):

add_filter( 'pll_enable_duplicate_media', '__return_false', 10 );

Editors can still reuse a single file across languages by switching the media modal’s filter to All languages.

Verify in 2 minutes

  1. Upload a JPEG and a PDF in your default language.
  2. Switch to another language → Media → Library.
    • The image should have a translation (pencil).
    • The PDF should show a “+” (no translation), meaning it won’t pollute other languages’ search results.

Reminder: Polylang duplicates attachment posts/meta, not the actual file. Deleting a translated attachment doesn’t delete the file on disk.

Hardening search (band-aid while you transition)

If your index already contains duplicated documents, keep WordPress search language-correct until you clean up:

add_action( 'pre_get_posts', function ( WP_Query $q ) {
    if ( $q->is_main_query() && $q->is_search() && function_exists( 'pll_current_language' ) ) {
        $q->set( 'lang', pll_current_language( 'slug' ) );
    }
});

pll_current_language() is provided by Polylang; for custom queries, also pass 'lang' => pll_current_language( 'slug' ).

What about existing duplicates?

The filter stops new duplicates only. To tidy up old ones:

  • In Media → List, look for document rows with many pencils; remove the non-needed language attachments (the original file remains).
  • For larger cleanups, script it (WP-CLI or SQL) by MIME type and slug—test on staging first and keep backups.

FAQ

Will this break featured images?
No—featured images are image/*, so they still get per-language attachments.

We use SVGs. Are they duplicated?
Yes, image/svg+xml matches the whitelist. Add an exception if you prefer to keep SVGs single-language.

Can I still create a per-language document when I need it?
Yes—click the “+” under that language in the Media list to create a single translated attachment on demand.