A successful blog is built with quality articles. To make the blog popular, we used to regularly write new and well-researched articles. Along with writing new posts, a blogger should also look into old articles. Updating old and outdated posts helps to improve your blog’s search rankings.
WordPress by default displays publish date on the post screen at the back-end. If we add the column which shows the last modified date for the post, it will help to easily track which posts need to be revised.
In this article, we study how to add a custom column called ‘Last Updated’ and display the last updated date on the post screen. The final output will look like the screenshot below.
How to Display Last Updated Date On Post Screen
We use this hook called manage_{$post_type}_post_columns
to add a custom column on the post screen. With this filter hook, one can add or remove columns on the post or page listing.
To add the column ‘Last Updated’ on the post listing, add the below code to the 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;
} );
Here I replaced {$post_type}
with post
value. In the case of a custom post type, we need to adjust this value.
Next, we need to display values of the last modified date for the column ‘Last Updated’. For this, use the below code.
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 );
Now we are done with displaying the last updated date column and its values.
In the next step, I’ll add a sorting option to this custom column. In this way, we can sort the posts in ascending or descending order against their modified date.
add_filter( 'manage_edit-post_sortable_columns', function( $columns ) {
$columns['last_updated'] = 'post_modified'; //'post_modified' is the column name in the wp_posts table
return $columns;
} );
The final code that goes inside the functions.php
file would be as follows.
// Add 'Last Updated' column
add_filter('manage_post_posts_columns', function ( $columns ) {
if( is_array( $columns ) && ! isset( $columns['last_updated'] ) ) {
$columns['last_updated'] = __( 'Last Updated' );
}
return $columns;
} );
// Display value for 'Last Updated' column
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 sorting to 'Last Updated' column
add_filter( 'manage_edit-post_sortable_columns', function( $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 displaying the last updated date on the post screen. It should help you to find old posts quickly. Please share your thoughts and suggestions in the comment section below.
Related Articles
- Load Dynamic Content on Bootstrap Modal in WordPress
- How to Add Custom Endpoints to WordPress REST API
- How to Add Image Field to Taxonomy in WordPress
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.