Content Marketing

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:

Date Modified / Edited Column in WordPress Posts Admin Page

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:
  • This function is responsible for adding a new column called “Date Edited” to the WordPress admin post list.
  • It receives an array $columns that represents the existing columns.
  • It creates a new array $new_columns to hold the reordered columns.
  • It iterates through the existing columns and adds them to the new array.
  • When it encounters the ‘date’ column (representing the “Published Date” column), it adds the “Date Edited” column right after it.
  • Finally, it returns the new array of columns, including the “Date Edited” column.
  1. add_filter('manage_edit-post_columns', 'mtz_custom_columns'):
  • This line hooks the mtz_custom_columns function to the ‘manage_edit-post_columns’ filter. It tells WordPress to run the function when the columns in the post edit screen are being managed.
// 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:
  • This function is responsible for displaying the content in the “Date Edited” column for each post.
  • It receives two parameters: $column (the current column being displayed) and $post_id (the ID of the current post).
  • It checks if the current column is ‘date_edited’ (the “Date Edited” column).
  • If it is, it retrieves the post’s modified date and time using get_post_field and stores it in the $post_modified variable.
  • It then formats the date and time as “YYYY/MM/DD at H:MM AM” using date_i18n, which takes into account the site’s date and time settings.
  • Finally, it echoes “Edited” on the first line and the formatted date and time on the second line, separated by a line break (<br>).
  1. add_action('manage_post_posts_custom_column', 'mtz_custom_column_content', 10, 2):
  • This line hooks the mtz_custom_column_content function to the ‘manage_post_posts_custom_column’ action. It specifies that the function should run when custom content needs to be displayed in a column for a post.
  • The function is hooked with a priority of 10 and accepts 2 parameters (the column and the post ID).
// 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:
  • This function is responsible for making the “Date Edited” column sortable.
  • It receives the array of sortable columns $columns.
  • It adds ‘date_edited’ as a sortable column and associates it with ‘post_modified’.
  • Finally, it returns the updated array of sortable columns.
  1. add_filter('manage_edit-post_sortable_columns', 'mtz_custom_sortable_columns'):
  • This line hooks the mtz_custom_sortable_columns function to the ‘manage_edit-post_sortable_columns’ filter. It tells WordPress that the “Date Edited” column can be sorted based on the ‘post_modified’ value.

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.

Appreciate this content?

Douglas Karr

Douglas Karr is CMO of OpenINSIGHTS and the founder of the Martech Zone. Douglas has helped dozens of successful MarTech startups, has assisted in the due diligence of over $5 bil in Martech acquisitions and investments, and continues to assist companies in implementing and automating their sales and marketing strategies. Douglas is an internationally recognized digital transformation and MarTech expert and speaker. Douglas is also a published author of a Dummie's guide and a business leadership book.
Back to top button
Close

Adblock Detected

Martech Zone is able to provide you this content at no cost because we monetize our site through ad revenue, affiliate links, and sponsorships. We would appreciate if you would remove your ad blocker as you view our site.