How to Sort Divi Gallery Images by Date or Newest First
If you use the Divi Gallery module, you may have noticed that divi does not give you the option to sort the images by upload date automatically. Pretty annoying.
For many websites, especially portfolios, event photo pages, or product galleries, having the newest images at the top makes the most sense. The good news is that there is a quick way to make this happen automatically
Can The Divi Gallery Sort By Date?
By default, the Divi Gallery module does not have a built-in option to sort images by date. Instead, it uses the order you selected them when adding the gallery. This can lead to outdated images appearing first, forcing you to manually rearrange them each time you add new ones.
This manual process takes time, and if you have a large gallery, it can be frustrating to keep everything in the right order.
Sorting Divi Gallery Images By Upload Date (Or by ID!)
In WordPress, every image has both a date and a unique ID number. Sorting by the upload date could give you the correct order, but it can run into problems if you upload multiple images on the same day, or even within the same second. In those cases, WordPress may assign the exact same timestamp to more than one image, which can cause the order to appear random.
Sorting by ID avoids that problem completely. IDs are unique and always increase as new images are added to the site. That means even if you upload 50 images in one batch, the one uploaded last will have the highest ID and will appear first when sorted in descending order. This makes ID-based sorting both faster and more reliable than date-based sorting for Divi galleries, and achieves the same effect.
Step-by-Step Guide to Sorting Divi Gallery Images by ID
You can fix the ordering issue with a short PHP snippet. We will also set it up so this only applies to galleries you want to change by adding a special CSS class in the Divi Builder.
Here is the code:
How to Use It
- Install the free Code Snippets plugin. (or a similar plugin)
- Create a new snippet, paste in the code above, and save it.
- Edit your page in Divi and open the Gallery module settings.
- In the Advanced tab, find CSS Class and enter: sort_by_date
Only galleries with that CSS class will be sorted automatically by newest first. All other galleries will remain in their original order.
By sorting your Divi galleries automatically, you avoid having to manually reorder images every time you upload new ones. This is especially useful for sites that frequently add new photos, such as portfolios, product catalogs, or event albums.
Using ID-based sorting ensures that even large batches of uploads will display in the correct order, no matter how many images share the same upload date. It is a set-it-and-forget-it solution that keeps your galleries fresh without extra work.

