How can I speed up a slow loading media library?

My WordPress site is fairly large. It’s being used as a blog and a CMS, with a WooCommerce shop running off it as well. There’s probably over 2000 images in the media library at this stage.

My problem is whenever new images need to be added to a post or a product, it’s glacially slow.

I have disabled every plugin on the website on a test deployment. The media library is still incredibly slow. I’ve also checked it isn’t loading full size images, it appears to be loading the 300×300 medium images.

Generally the site has acceptable load times for pages and in the admin. They could be quicker, but they aren’t more than a second or two for the worst pages.

I think the main issue is whenever the add media button is clicked, the library loads every image ever put on the site. List view is much, much quicker. But this type of view isn’t available when adding an image in a post – or when uploading a new product image, it defaults to the thumbnails that show all images.

What can I do to speed up the media library?

1 Answer
1

I’m copying a previous answer here as I think it will help. What this will do is whenever the media library pops up, instead of showing all media for a given post it will filter down to only images attached to the post. You could paste this right into your functions file:

/**
 * Media Module - Only Show Images Uploaded To this Post
 */
function wpse222583_media_library_filter() { 
    ?>
      <script type="text/javascript">
        jQuery( document ).on( 'DOMNodeInserted', function() {
            jQuery( 'select.attachment-filters [value="uploaded"]' ).attr( 'selected', true ).parent().trigger( 'change' );
        } );
      </script>
    <?php 
}
add_action( 'admin_footer-post-new.php', 'wpse222583_media_library_filter' );
add_action( 'admin_footer-post.php', 'wpse222583_media_library_filter' );

The above doesn’t remove the filter from the media module so if you need to see all the media you can just change the filter while in the admin panel, this just presets it to only show attached media.

Media Library Filter Dropdown List

If this doesn’t work there’s a ton of other answers to this older question which I feel is related.

Leave a Comment