In case you’re a long-time reader of Martech Zone, you’ve got in all probability seen the work I’ve executed to take away outdated articles and replace widespread however outdated articles. Once I work on my posts web page within the WordPress admin, I filter the view considerably to determine articles that must be deleted or up to date.
One of many fields I wanted was the flexibility to type the view by date modified. I used to be stunned that this wasn’t an possibility, so I wrote the next code.
Add modified date in posts with sorting
This code provides an edited column to the WordPress admin publish listing utilizing WordPress API, shows it subsequent to the revealed date, shows the modified date and time within the desired format, and makes the column sortable based mostly on the modified date. Add this to your features.php file in your Children’s theme:
// Add column with edit date perform mtz_custom_columns($columns) { // Create new array to carry the rearranged columns $new_columns = array(); // Add all columns earlier than the edit date column foreach ($columns as $key => $worth) { $new_columns[$key] = $worth; if ($key === ‘date’) { // Add the Edited column proper after the Printed Date column $new_columns[‘date_edited’] = ‘Edited’; } } return $new_columns; } add_filter(‘manage_edit-post_columns’, ‘mtz_custom_columns’); // Present edit date worth perform mtz_custom_column_content($column, $post_id) { if ($column === ‘date_edited’) { $post_modified = get_post_field(‘post_modified’, $post_id); // Format date and time as “YYYY/MM/DD at 12:00 AM” with line breaks $formatted_date = date_i18n(‘Y/m/d atg:i A’, strtotime($post_modified)); echo ‘Edited
‘ . $formatted_date; } } add_action(‘manage_post_posts_custom_column’, ‘mtz_custom_column_content’, 10, 2); // Make the “Edit date” column sortable perform 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 publish view
And right here is the consequence:
Code rationalization
Let’s break down the supplied code intimately and clarify every half and its goal:
// Add column with edit date perform mtz_custom_columns($columns) { // Create new array to carry the rearranged columns $new_columns = array(); // Add all columns earlier than the edit date column foreach ($columns as $key => $worth) { $new_columns[$key] = $worth; if ($key === ‘date’) { // Add the Edited column proper after the Printed Date column $new_columns[‘date_edited’] = ‘Edited’; } } return $new_columns; } add_filter(‘manage_edit-post_columns’, ‘mtz_custom_columns’); mtz_custom_columns perform: This perform is chargeable for including a brand new column named Edited Date to the WordPress admin posts listing. It will get an array $columns representing the present columns. It creates a brand new array $new_columns containing the reordered columns. It loops by the present columns and provides them to the brand new array. When it hits the Date column (which represents the Printed Date column), it provides the Edited Date column proper after it. Lastly, the brand new column array is returned, together with the Edited Date column. add_filter(‘manage_edit-post_columns’, ‘mtz_custom_columns’): This line associates the mtz_custom_columns perform with the manage_edit-post_columns filter. It tells WordPress to run the perform when managing the columns within the post-processing display. // Present edit date worth perform mtz_custom_column_content($column, $post_id) { if ($column === ‘date_edited’) { $post_modified = get_post_field(‘post_modified’, $post_id); // Format date and time as “YYYY/MM/DD at 12:00 AM” with line breaks $formatted_date = date_i18n(‘Y/m/d atg:i A’, strtotime($post_modified)); echo ‘Edited
‘ . $formatted_date; } } add_action(‘manage_post_posts_custom_column’, ‘mtz_custom_column_content’, 10, 2); mtz_custom_column_content perform: This perform is chargeable for displaying the content material within the edit date column for every publish. It receives two parameters: $column (the presently displayed column) and $post_id (the ID of the present publish). It checks whether or not the present column is “date_edited” (the “edited date” column). If so, get_post_field will retrieve the publish’s modified date and time and retailer it within the $post_modified variable. It then Formats the date and time as “YYYY/MM/DD at H:MM AM” with date_i18n, which honors the location’s date and time settings. Lastly, the primary line shows “Edited” and the second line shows the formatted date and time, separated by a line break (
). add_action(‘manage_post_posts_custom_column’, ‘mtz_custom_column_content’, 10, 2): This line associates the mtz_custom_column_content perform with the motion ‘manage_post_posts_custom_column’. It specifies that the perform must be executed when customized content material must be displayed in a column for a publish. The perform is hooked with a precedence of 10 and accepts 2 parameters (the column and the publish ID). // Make the “Edit date” column sortable perform mtz_custom_sortable_columns($columns) { $columns[‘date_edited’] = ‘post_modified’; return $columns; } add_filter(‘manage_edit-post_sortable_columns’, ‘mtz_custom_sortable_columns’); mtz_custom_sortable_columns perform: This perform is chargeable for making the edit date column sortable. It will get the array of sortable columns $columns. It provides date_edited as a sortable column and associates it with post_modified. Lastly, the up to date array of sortable columns is returned. add_filter(‘manage_edit-post_sortable_columns’, ‘mtz_custom_sortable_columns’): This line associates the mtz_custom_sortable_columns perform with the filter “manage_edit-post_sortable_columns”. It tells WordPress that the Modified Date column could be sorted based mostly on the post_modified worth.
In case you want help with WordPress improvement, attain out high bridge, my firm. We may also help with customized theme improvement, plugin improvement, optimization, efficiency and extra.