WordPress – How to add “Last Modified” column to page and post lists

The following code shows how to add a “Last Modified” column to the page and post lists in WordPress.

The “Last Modified” column will be sortable, allowing you to order the posts or pages by latest or oldest.

Note: I highly recommend saving PHP code into a custom plugin – this way you can easily enable/disable the customisations and keep them when you update or replace your theme. See How to create a WordPress plugin for your custom functions for how to do this.

 

// Add the custom column to the post type
add_filter( 'manage_pages_columns', 'itsg_add_custom_column' );
add_filter( 'manage_posts_columns', 'itsg_add_custom_column' );
function itsg_add_custom_column( $columns ) {
    $columns['modified'] = 'Last Modified';

    return $columns;
}

// Add the data to the custom column
add_action( 'manage_pages_custom_column' , 'itsg_add_custom_column_data', 10, 2 );
add_action( 'manage_posts_custom_column' , 'itsg_add_custom_column_data', 10, 2 );
function itsg_add_custom_column_data( $column, $post_id ) {
    switch ( $column ) {
        case 'modified' :
			$date_format = 'Y/m/d';
			$post = get_post( $post_id );
			echo get_the_modified_date( $date_format, $post ); // the data that is displayed in the column
            break;
    }
}

// Make the custom column sortable
add_filter( 'manage_edit-page_sortable_columns', 'itsg_add_custom_column_make_sortable' );
add_filter( 'manage_edit-post_sortable_columns', 'itsg_add_custom_column_make_sortable' );
function itsg_add_custom_column_make_sortable( $columns ) {
	$columns['modified'] = 'modified';

	return $columns;
}

// Add custom column sort request to post list page
add_action( 'load-edit.php', 'itsg_add_custom_column_sort_request' );
function itsg_add_custom_column_sort_request() {
	add_filter( 'request', 'itsg_add_custom_column_do_sortable' );
}

// Handle the custom column sorting
function itsg_add_custom_column_do_sortable( $vars ) {
	// check if sorting has been applied
	if ( isset( $vars['orderby'] ) && 'modified' == $vars['orderby'] ) {

		// apply the sorting to the post list
		$vars = array_merge(
			$vars,
			array(
				'orderby' => 'post_modified'
			)
		);
	}

	return $vars;
}