For those who’ve been a long-time reader of Martech Zone, you’ve probably observed the work I’ve been doing to take away outdated articles and replace articles which might be widespread 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 possibility, so I wrote the next code.
Add Date Modified In Posts With Type
This code provides an Edited column to the WordPress admin publish record 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 baby theme:
// Add Date Edited Column
perform 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 "Printed Date" column
$new_columns['date_edited'] = 'Edited';
}
}
return $new_columns;
}
add_filter('manage_edit-post_columns', 'mtz_custom_columns');
// Show Date Edited Worth
perform 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
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 Posts View
And right here’s the end result:
Code Clarification
Let’s break down the offered code intimately, explaining every half and its objective:
// Add Date Edited Column
perform 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 "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 answerable for including a brand new column known as “Date Edited” to the WordPress admin publish record.
- 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 via the prevailing columns and provides them to the brand new array.
- When it encounters the ‘date’ column (representing the “Printed 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.
add_filter('manage_edit-post_columns', 'mtz_custom_columns')
:
- This line hooks the
mtz_custom_columns
perform to the ‘manage_edit-post_columns’ filter. It tells WordPress to run the perform when the columns within the publish edit display are being managed.
// Show Date Edited Worth
perform 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);
mtz_custom_column_content
perform:
- This perform 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 into 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>
).
add_action('manage_post_posts_custom_column', 'mtz_custom_column_content', 10, 2)
:
- This line hooks the
mtz_custom_column_content
perform to the ‘manage_post_posts_custom_column’ motion. It specifies that the perform ought to run 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 Date Edited 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 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.
add_filter('manage_edit-post_sortable_columns', 'mtz_custom_sortable_columns')
:
- This line hooks the
mtz_custom_sortable_columns
perform to the ‘manage_edit-post_sortable_columns’ filter. It tells WordPress that the “Date Edited” column may be sorted based mostly on the ‘post_modified’ worth.
For those who want WordPress improvement help, contact Highbridge, my agency. We are able to help with customized theme improvement, plugin improvement, optimization, efficiency, and extra.