WordPress: How To Add A Modified Date Column To Your Posts View And Make It Sortable

If you’ve been a long-time reader of Martech Zone, you have likely noticed the work I’ve been doing to remove outdated articles and update articles that are popular but outdated. When working within my posts page in WordPress admin, I filter the view significantly to identify articles to delete or update.

One of the fields that I needed was the ability to sort the view based on the modified date. I was surprised this wasn’t an option, so I wrote the following code.

Add Date Modified In Posts With Sort

This code adds an Edited column to the WordPress admin post list using the WordPress API, displays it adjacent to the published date, displays the modified date and time in the desired format, and makes the column sortable based on the modification date. Add this to your functions.php file in your child theme:

// Add Date Edited Column
function mtz_custom_columns($columns) {
    // Create a new array to hold the reordered columns
    $new_columns = array();

    // Add all columns before the "Date Edited" column
    foreach ($columns as $key => $value) {
        $new_columns[$key] = $value;
        if ($key === 'date') {
            // Add the "Edited" column right after the "Published Date" column
            $new_columns['date_edited'] = 'Edited';
        }
    }

    return $new_columns;
}
add_filter('manage_edit-post_columns', 'mtz_custom_columns');

// Display Date Edited Value
function mtz_custom_column_content($column, $post_id) {
    if ($column === 'date_edited') {
        $post_modified = get_post_field('post_modified', $post_id);
        
        // Format the date and time as "YYYY/MM/DD at 0:00 AM" with line breaks
        $formatted_date = date_i18n('Y/m/d \a\t g:i A', strtotime($post_modified));
        
        echo 'Edited<br>' . $formatted_date;
    }
}
add_action('manage_post_posts_custom_column', 'mtz_custom_column_content', 10, 2);

// Make Date Edited Column Sortable
function mtz_custom_sortable_columns($columns) {
    $columns['date_edited'] = 'post_modified';
    return $columns;
}
add_filter('manage_edit-post_sortable_columns', 'mtz_custom_sortable_columns');

WordPress Admin Posts View

And here’s the result:

Code Explanation

Let’s break down the provided code in detail, explaining each part and its purpose:

// Add Date Edited Column
function mtz_custom_columns($columns) {
    // Create a new array to hold the reordered columns
    $new_columns = array();

    // Add all columns before the "Date Edited" column
    foreach ($columns as $key => $value) {
        $new_columns[$key] = $value;
        if ($key === 'date') {
            // Add the "Edited" column right after the "Published Date" column
            $new_columns['date_edited'] = 'Edited';
        }
    }

    return $new_columns;
}
add_filter('manage_edit-post_columns', 'mtz_custom_columns');
  1. mtz_custom_columns function:
  1. add_filter('manage_edit-post_columns', 'mtz_custom_columns'):
// Display Date Edited Value
function mtz_custom_column_content($column, $post_id) {
    if ($column === 'date_edited') {
        $post_modified = get_post_field('post_modified', $post_id);

        // Format the date and time as "YYYY/MM/DD at 0:00 AM" with line breaks
        $formatted_date = date_i18n('Y/m/d \a\t g:i A', strtotime($post_modified));

        echo 'Edited<br>' . $formatted_date;
    }
}
add_action('manage_post_posts_custom_column', 'mtz_custom_column_content', 10, 2);
  1. mtz_custom_column_content function:
  1. add_action('manage_post_posts_custom_column', 'mtz_custom_column_content', 10, 2):
// Make Date Edited Column Sortable
function mtz_custom_sortable_columns($columns) {
    $columns['date_edited'] = 'post_modified';
    return $columns;
}
add_filter('manage_edit-post_sortable_columns', 'mtz_custom_sortable_columns');
  1. mtz_custom_sortable_columns function:
  1. add_filter('manage_edit-post_sortable_columns', 'mtz_custom_sortable_columns'):

If you need WordPress development assistance, contact DK New Media, my firm. We can assist with custom theme development, plugin development, optimization, performance, and more.

Exit mobile version