Reorder attachements in the media library

I have a plugin that takes an user selected image from the media library, puts some special effects on it, and then saves a copy of the “enhanced” image back to the library using media_handle_upload().

This works fine, but it puts the updated image at the top of the media library, separating it from the original.

I want to keep the enhanced attachment next to the original, and I learned from this question that data for post attachments is mainly stored in the wp_posts table.

However I can’t find what column in the wp_posts is used to sort attachments when the wordpress media library displays them.

Any help with this problem would be much appreciated, thank you!

1 Answer

Attachments in Media Library are obtained using WP_Query, so you can modify that query with pre_get_posts action.

By default they are sorted DESC by post_date.

So the easiest way to fix your code would be to change so the new modified attachment has the same post_date set as the original one.

Leave a Comment