# Sort Admin Lists by Last Modified by Default

> Lists recently edited posts first in the admin, unless you click a column to sort differently.

- Type: PHP
- Runs: Admin only
- Category: [Admin](https://wpide.com/library/category/admin/)
- Tags: admin, list-table, sorting
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/admin-sort-by-modified-default/

## Code

```php
// Post types whose admin list should default to "last modified first".
$wpide_sort_modified_types = array( 'post' );

add_action( 'pre_get_posts', function ( $query ) use ( $wpide_sort_modified_types ) {
    global $pagenow;

    if ( ! is_admin() || ! $query->is_main_query() || 'edit.php' !== $pagenow || isset( $_GET['orderby'] ) ) {
        return;
    }

    $post_type = $query->get( 'post_type' ) ? $query->get( 'post_type' ) : 'post';

    if ( in_array( $post_type, $wpide_sort_modified_types, true ) ) {
        $query->set( 'orderby', 'modified' );
        $query->set( 'order', 'DESC' );
    }
} );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Sort Admin Lists by Last Modified by Default" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
