One recent client began populating their site with huge images… JPGs over 5MB and PNGs over 10MB. In an effort to root out some of these outrageous images I wanted to view the file sizes in their Media Library and be able to sort them all by size in ascending or descending order.
The basic steps to accomplish this were:
postmetafor each current attachment in the Media Library (I’m not using attachment metadata because this gets saved as a serialized array which will make our sorting tricky)
- Ensure each time a new image is uploaded to the Media Library that its
filesizeis saved in its
- Create the new Media Library column
- Populate the new column with its data
- Make the new column sortable
- Define our new column’s sorting logic
For Step 1, I created a function that should only be run once. It’s a simple custom query that iterates through all attachments that don’t have
filesize meta set yet. It’s commented out in my Gist because it can cause issues on your site if you have a large Media Library. Use it cautiously and use it once.
The remaining steps are all here:
I hope this helps anyone looking for more ways to organize their Media Library!