Monday, September 11, 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


If you happen to’ve been a long-time reader of Martech Zone, you will have probably seen the work I’ve been doing to take away outdated articles and replace articles which might be well-liked 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 power to kind the view primarily based 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 publish checklist utilizing the WordPress API, shows it adjoining to the revealed date, shows the modified date and time within the desired format, and makes the column sortable primarily based 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 Clarification

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

// 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 answerable for including a brand new column known as “Date Edited” to the WordPress admin publish checklist.
  • 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 way of 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 publish 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 answerable for displaying the content material within the “Date Edited” column for every publish.
  • It receives two parameters: $column (the present column being displayed) and $post_id (the ID of the present publish).
  • It checks if the present column is ‘date_edited’ (the “Date Edited” column).
  • Whether it is, it retrieves the publish’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 publish.
  • The operate is hooked with a precedence of 10 and accepts 2 parameters (the column and the publish 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 answerable 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 may be sorted primarily based on the ‘post_modified’ worth.

If you happen to want WordPress growth help, contact Highbridge, my agency. We will help with customized theme growth, plugin growth, optimization, efficiency, and extra.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments