Display Last Updated Date On Post Screen In WordPress

A successful blog is built upon the number of quality posts. We used to write new post regularly. While writing a new post, a blogger should take care of old posts also. By updating old post will help to improve your search rankings.

WordPress by default displays publish date on the post screen at the back-end. If we add the column which shows when the posts are last modified, it will helpful to track which post should update now.

In this article, we study how to add custom column ‘Last Updated’ and display last updated date on post screen in WordPress.

Display Last Updated Date On Post Screen

How To Display Last Updated Date On Post Screen

To add a custom column on post screen we use the hook manage_post_{$post_type}_columns provided by WordPress. This filter can use to add or remove columns from post or page table. To add our column ‘Last Updated’ on post table add the below code in functions.php file.

add_filter('manage_post_posts_columns', function ( $columns ) 
{
	if( is_array( $columns ) && ! isset( $columns['last_updated'] ) )
	    $columns['last_updated'] = __( 'Last Updated' );

	return $columns;
} );

Next, we need to display the last modified date under the column ‘Last Updated’. For this, place the below code in the functions.php file.

add_action( 'manage_post_posts_custom_column', function ( $column_name, $post_id ) 
{
    if ( $column_name == 'last_updated') {
    	echo get_the_modified_date(get_option('date_format'), $post_id);
    }

}, 10, 2 );

At this stage, we have completed with the display of last updated date and column.

In the next step, we will add sorting option on our custom column. By this way, we can sort the post in ascending or descending order on the basis of post modified date.

add_filter( 'manage_edit-post_sortable_columns', 'last_updated_sortable_column' );
function last_updated_sortable_column( $columns ) {
    $columns['last_updated'] = 'post_modified'; //'post_modified' is the column name in the wp_posts table
 
    return $columns;
}

Our final code goes in functions.php file would be as follows.

add_filter('manage_post_posts_columns', function ( $columns ) 
{
	if( is_array( $columns ) && ! isset( $columns['last_updated'] ) )
	    $columns['last_updated'] = __( 'Last Updated' );

	return $columns;
} );

add_action( 'manage_post_posts_custom_column', function ( $column_name, $post_id ) 
{
    if ( $column_name == 'last_updated') {
    	echo get_the_modified_date(get_option('date_format'), $post_id);
    }

}, 10, 2 );

add_filter( 'manage_edit-post_sortable_columns', 'last_updated_sortable_column' );
function last_updated_sortable_column( $columns ) {
    $columns['last_updated'] = 'post_modified'; //'post_modified' is the column name in the wp_posts table
 
    return $columns;
}

That’s it! We are done with the display last updated date on post screen. You may also want to check our article on How To Display Post Views Count On Post Screen In WordPress.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *