Sort Admin Lists by Last Modified by Default
Lists recently edited posts first in the admin, unless you click a column to sort differently.
Opens WPIDE on
Code
// 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 add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.