Tuesday, November 21, 2023
HomeMobile MarketingWordPress: How To Add A Modified Date Column To Your Posts View...

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


For those who’ve been a long-time reader of Martech Zone, you have got seemingly seen the work I’ve been doing to take away outdated articles and replace articles which are fashionable however outdated. When working inside my posts web page in WordPress admin, I filter the view considerably to determine articles to delete or replace.

One of many fields that I wanted was the flexibility to kind the view based mostly on the modified date. I used to be shocked this wasn’t an choice, so I wrote the next code.

Add Date Modified In Posts With Type

This code provides an Edited column to the WordPress admin submit listing utilizing the WordPress API, shows it adjoining to the printed date, shows the modified date and time within the desired format, and makes the column sortable based mostly on the modification date. Add this to your capabilities.php file in your youngster theme:

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

    // Add all columns earlier than the "Date Edited" column
    foreach ($columns as $key => $worth) {
        $new_columns[$key] = $worth;
        if ($key === 'date') {
            // Add the "Edited" column proper after the "Revealed Date" column
            $new_columns['date_edited'] = 'Edited';
        }
    }

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

// Show Date Edited Worth
operate 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 at 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
operate 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 right here’s the outcome:

Date Modified / Edited Column in WordPress Posts Admin Page

Code Rationalization

Let’s break down the supplied code intimately, explaining every half and its function:

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

    // Add all columns earlier than the "Date Edited" column
    foreach ($columns as $key => $worth) {
        $new_columns[$key] = $worth;
        if ($key === 'date') {
            // Add the "Edited" column proper after the "Revealed Date" column
            $new_columns['date_edited'] = 'Edited';
        }
    }

    return $new_columns;
}
add_filter('manage_edit-post_columns', 'mtz_custom_columns');
  1. mtz_custom_columns operate:
  • This operate is liable for including a brand new column known as “Date Edited” to the WordPress admin submit listing.
  • It receives an array $columns that represents the prevailing columns.
  • It creates a brand new array $new_columns to carry the reordered columns.
  • It iterates by the prevailing columns and provides them to the brand new array.
  • When it encounters the ‘date’ column (representing the “Revealed Date” column), it provides the “Date Edited” column proper after it.
  • Lastly, it returns the brand new array of columns, together with the “Date Edited” column.
  1. add_filter('manage_edit-post_columns', 'mtz_custom_columns'):
  • This line hooks the mtz_custom_columns operate to the ‘manage_edit-post_columns’ filter. It tells WordPress to run the operate when the columns within the submit edit display are being managed.
// Show Date Edited Worth
operate 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 at 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 operate:
  • This operate is liable for displaying the content material within the “Date Edited” column for every submit.
  • It receives two parameters: $column (the present column being displayed) and $post_id (the ID of the present submit).
  • It checks if the present column is ‘date_edited’ (the “Date Edited” column).
  • Whether it is, it retrieves the submit’s modified date and time utilizing get_post_field and shops it within the $post_modified variable.
  • It then codecs the date and time as “YYYY/MM/DD at H:MM AM” utilizing date_i18n, which takes under consideration the positioning’s date and time settings.
  • Lastly, it echoes “Edited” on the primary 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 operate to the ‘manage_post_posts_custom_column’ motion. It specifies that the operate ought to run when customized content material must be displayed in a column for a submit.
  • The operate is hooked with a precedence of 10 and accepts 2 parameters (the column and the submit ID).
// Make Date Edited Column Sortable
operate 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 operate:
  • This operate is liable for making the “Date Edited” column sortable.
  • It receives the array of sortable columns $columns.
  • It provides ‘date_edited’ as a sortable column and associates it with ‘post_modified’.
  • Lastly, it returns the up to date array of sortable columns.
  1. add_filter('manage_edit-post_sortable_columns', 'mtz_custom_sortable_columns'):
  • This line hooks the mtz_custom_sortable_columns operate to the ‘manage_edit-post_sortable_columns’ filter. It tells WordPress that the “Date Edited” column will be sorted based mostly on the ‘post_modified’ worth.

For those who want WordPress improvement help, contact Highbridge, my agency. We will help with customized theme improvement, plugin improvement, optimization, efficiency, and extra.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments