By default, WordPress doesn’t provide a built-in option to sort media attachments by file name in the media library interface. However, you can achieve this programmatically or with the help of plugins.
-
Using Plugins: Plugins like “Media Library Assistant” or “Media Library Categories” offer advanced sorting and filtering options for the media library, including sorting by file name.
-
Custom Code: If you prefer custom development, you can use WordPress hooks and filters to modify the query parameters for retrieving media attachments and sort them by file name.
Here’s a basic example:
function custom_media_library_sort_by_filename($query) {
if (is_admin() && $query->is_main_query() && $query->get('post_type') == 'attachment') {
$query->set('orderby', 'title');
$query->set('order', 'ASC'); // or 'DESC' for descending order
}
}
add_action('pre_get_posts', 'custom_media_library_sort_by_filename');
This code snippet hooks into the pre_get_posts action to modify the query parameters for retrieving media attachments. It sets the orderby parameter to ‘title’ (which represents the file name) and specifies the sorting order.
Whether you choose to use a plugin or custom code depends on your preferences and the specific requirements of your website. Plugins offer convenience and often come with additional features, while custom code provides more flexibility and control over the implementation.