WPSurfer.com

How to Add a Modified Date to Posts in Admin without a Plugin

Published on November 4, 2024

When managing a WordPress website, it’s often helpful to see when content was last modified, especially for sites that frequently update their posts and pages.

In this post, I’ll walk through a simple plugin that adds a “Modified Date” column to the WordPress admin post and page lists.

This column will not only show the last modified date but also allow you to sort posts and pages based on that date.


Mu-Plugin

We are gonna use a mu-plugin for that but you can easily remove the PHP opening tag and the plugin information and implement this feature via your favorite code snippets plugin.

I am such a big fan of using mu-plugin because you don’t really need to rely on an extra plugin for that.

<?php

/*
  Plugin Name: Modified Date Grabber
  Description: Add Last Modified Date to Posts and Pages List
  Version: 1.0
  Author: TicoLibre
*/

// Add modified date column to posts and pages lists
function custom_modified_date_column($defaults) {
    $defaults['modified_date'] = 'Modified Date';
    return $defaults;
}

function custom_modified_date_column_content($column_name, $post_ID) {
    if ($column_name == 'modified_date') {
        $modified_date = get_post_field('post_modified', $post_ID);
        $formatted_date = date_i18n('Y/m/d \a\t g:i a', strtotime($modified_date));
        echo 'Modified ' . '<br>' . esc_html($formatted_date);
    }
}

function custom_modified_date_column_sortable($columns) {
    $columns['modified_date'] = 'modified_date';
    return $columns;
}

function custom_sortable_columns_orderby($query) {
    if (!is_admin() || !$query->is_main_query()) {
        return;
    }

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

    if ($orderby == 'modified_date') {
        $query->set('orderby', 'modified');
        $query->set('order', 'desc'); // Set the order to descending
    }
}

add_filter('manage_posts_columns', 'custom_modified_date_column');
add_action('manage_posts_custom_column', 'custom_modified_date_column_content', 10, 2);
add_filter('manage_pages_columns', 'custom_modified_date_column'); // Add this line to include the column in pages list
add_action('manage_pages_custom_column', 'custom_modified_date_column_content', 10, 2);
add_filter('manage_edit-post_sortable_columns', 'custom_modified_date_column_sortable'); // Add this line to make the column sortable in posts list
add_filter('manage_edit-page_sortable_columns', 'custom_modified_date_column_sortable'); // Add this line to make the column sortable in pages list
add_action('pre_get_posts', 'custom_sortable_columns_orderby');

Code Breakdown

These are some of the technical details about how this mu-plugin works

  • The function custom_modified_date_column adds a new column titled “Modified Date” to the posts and pages list views.
  • The function custom_modified_date_column_content retrieves and formats the modified date of each post or page, displaying it in a user-friendly format (e.g., “2023/05/15 at 10:30 am”).
  • The function custom_modified_date_column_sortable makes the new “Modified Date” column sortable in the posts and pages lists, allowing users to click on the column heading to sort by modified date.

Manuel Campos

Manuel Campos

I am José Manuel. I am writing about things I know and things that I am learning about WordPress. I hope you find the content of this blog useful.