Skip to content

WordPress media library — managing 100k+ items without the admin crawl

A WordPress media library over 100,000 items breaks the admin grid, the date filter, and most plugins. Operating practices that keep the library searchable and the editor fast.

A news publisher’s WordPress site crosses 100,000 attachments and three things stop working at once: the media library grid takes 20 seconds to load, the search field returns nothing for queries that should have hits, and the editor’s “Add media” modal hangs the browser tab. The library still works in theory — every attachment is a wp_posts row with post_type=attachment — but the admin UI was designed when 5,000 attachments was the upper bound. There is no single fix; there is a set of operating practices.

What’s happening

The media grid loads via the attachments REST endpoint, which runs a WP_Query against wp_posts filtered by post_type=attachment, ordered by post_date DESC, paginated 80 at a time. The query joins wp_postmeta for the attached file path and the metadata. On a library with 100k+ attachments and a wp_postmeta table that has grown to several million rows, the query is no longer instant. The date filter dropdown runs a separate GROUP BY YEAR(post_date), MONTH(post_date) query that has no index and full-scans the table.

The search field uses LIKE '%term%' against post_title and the GUID — there is no full-text index, no MeiliSearch, no Elasticsearch. Anything beyond an exact filename or title prefix is unreliable.

How to verify

wp media list --format=count
wp db query "SELECT COUNT(*) FROM wp_posts WHERE post_type='attachment';"
wp db query "SELECT COUNT(*) FROM wp_postmeta WHERE meta_key LIKE '_wp_attach%';"
wp db query "EXPLAIN SELECT * FROM wp_posts WHERE post_type='attachment' ORDER BY post_date DESC LIMIT 80;"
tail -f /var/log/mysql/slow.log

Open the media library grid in the admin. Open DevTools > Network. Reload. The /wp/v2/media call should return under 1 second; anything over 3 seconds is a problem the editorial team will feel.

The fix

  1. Add a covering index on wp_posts. CREATE INDEX wp_posts_type_date ON wp_posts (post_type, post_date DESC); cuts the library load from seconds to milliseconds on most libraries. Test on staging before applying to production.
  2. Disable the date filter dropdown. It triggers the unindexed GROUP BY query on every page load. A small mu-plugin can remove it from the media grid screen.
  3. Replace search with a proper engine. Install ElasticPress or SearchWP. Both index attachments and offer real full-text search. SearchWP is more turnkey; ElasticPress requires running an Elasticsearch or OpenSearch cluster but scales further.
  4. Move to list view by default. wp user meta update <user-id> wp_media_library_mode list switches the user to the table view, which paginates more efficiently than the grid. For editorial teams, list view is also faster to scan visually.
  5. Archive cold media. Attachments tied to posts older than five years and never referenced by current content can be moved to a wp-content/uploads-archive/ path that the media library doesn’t index. The enterprise WordPress operations operating model includes attachment lifecycle policies as part of standard onboarding.

When the WordPress media library has crossed six figures and the editorial team is losing minutes per session waiting for the admin to respond, Stack Harbor’s enterprise WordPress operations team sizes the right intervention — start the conversation for a sizing pass.