Add a “Last Updated” Column To The WP Backend (Works for Posts & Pages)

I’m currently updating posts for one of my sites and I hit a tiny snag. I wanted to easily see the last updated date for each of my posts on the All Posts page in the WordPress dashboard (the /wp-admin/edit.php page).

WordPress doesn’t seem to provide an out of the box solution so with ChatGPT’s help, I created the necessary code.

Updated to work on posts and pages.

Here’s the end result:

Add "Last Updated" Column To The WP Backend
The Last Updated Column should be standard but as of January 2023, it’s not.

PHP Code To Show The Last Updated Column in WordPress All Posts and All Pages

It’s reasonably straight-forward to achieve.

Simply add some code to your functions.php file OR, use the FREE Code Snippets plugin from WPCode.

I opted for the plugin route because I have various code snippets and this amazing little plugin keeps everything organized. It offers granular control over exactly where the code can execute and it allows me to switch on or off various code snippets as desired.

All for free! There is a pro version too. I might check that out at a later date.

By the way, the plugin used to be called Insert Headers and Footers. At time of writing, it has 1+ million installs, so it’s very popular.

Anyway, here’s the code snippet:

// Adds a new sortable "last updated" column to posts and pages backend.
function custom_columns($defaults) {
    $defaults['last_updated'] = __('Last Updated', 'your-textdomain');
    return $defaults;
}
add_filter('manage_posts_columns', 'custom_columns');
add_filter('manage_pages_columns', 'custom_columns');

function custom_columns_content($column_name, $post_id) {
    if ($column_name == 'last_updated') {
        $last_updated = get_the_modified_date();
        echo $last_updated;
    }
}
add_action('manage_posts_custom_column', 'custom_columns_content', 10, 2);
add_action('manage_pages_custom_column', 'custom_columns_content', 10, 2);

function custom_columns_sortable($columns) {
    $columns['last_updated'] = 'last_updated';
    return $columns;
}
add_filter('manage_edit-post_sortable_columns', 'custom_columns_sortable');
add_filter('manage_edit-page_sortable_columns', 'custom_columns_sortable');

function custom_columns_orderby($query) {
    if (!is_admin()) {
        return;
    }

    $orderby = $query->get('orderby');

    if ('last_updated' == $orderby) {
        $query->set('orderby', 'modified');
    }
}
add_action('pre_get_posts', 'custom_columns_orderby');

Here’s how to get the Last Updated column working for the backend your WordPress site:

  1. Install and activate the free Code Snippets plugin
  2. On the side menu, click on Add Snippet
  3. Select Add Your Custom Code (New Snippet)
  4. Paste the code above into the Code Preview box
  5. Select PHP Snippet as the Code Type from the dropdown box
  6. Insure the Auto Insert option is selected from Insert Method
  7. Select Admin Only as the location
  8. Go back to the top, toggle the Inactive button to show Active
  9. Click Save Snippet
  10. Visit your Posts (all posts page) and you should see the Last Updated Column

If this doesn’t work, sorry, I cannot offer support. It works perfectly for me and I have a minimal setup. If it doesn’t work for you, chances are that another plugin might be causing a conflict.

Enjoy!

Similar Posts