WordPress: Add Featured Image and a Sortable Last Edit Date To WordPress Admin

We’re launching a new WordPress site I designed and developed at my company. Having developed and deployed hundreds of WordPress sites, I always install some essential plugins and make customizations to manage the site more effectively. One example is the admin panel’s page, posts, and custom post type (CPT) lists. I like ensuring each entry has a featured image, and I often want to sort the list by when it was last edited, not necessarily when it was published.

WordPress: Featured Image and Last Edit Column (Sortable)

Neither of those columns exists, but they can be easily added using either a child theme functions.php file or by building a custom plugin. As a common practice, I like to develop a custom WordPress plugin for each install so that functionality persists even if I change themes on the site.

WordPress Plugin: Featured Image and Last Edit Date (Sortable)

This plugin adds a Featured Image column (only if the post type supports thumbnails) and a Last Edited column to posts, pages, and all custom post types.

<?php
/*
Plugin Name: Admin Post Columns Enhancer
Plugin URI:  https://dknewmedia.com
Description: Adds "Featured Image" (if supported) and "Last Edited" columns to posts, pages, and custom post types in the admin panel.
Version:     1.0.0
Author:      Douglas Karr
Author URI:  https://dknewmedia.com
License:     GPL2
License URI: https://www.gnu.org/licenses/gpl-2.0.html
Text Domain: admin-post-columns-enhancer
*/

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

/**
 * Get all registered public post types, including posts and pages.
 * If you are using this code in a theme, use everything after this comment
 */
function apce_get_all_post_types() {
    $args = [
        'public'   => true,
        '_builtin' => false,
    ];
    $post_types = get_post_types($args, 'names');

    // Include 'post' and 'page' as well
    $post_types[] = 'post';
    $post_types[] = 'page';

    return $post_types;
}

/**
 * Modify admin post list columns to include "Featured Image" and "Last Edited".
 */
function apce_add_custom_columns($columns, $post_type) {
    $new_columns = [];

    // Add Featured Image column only if the post type supports thumbnails
    $supports_thumbnails = post_type_supports($post_type, 'thumbnail');

    foreach ($columns as $key => $value) {
        if ($key === 'cb') { // Checkbox column
            $new_columns[$key] = $value;
            if ($supports_thumbnails) {
                $new_columns['featured_image'] = __('Featured Image', 'admin-post-columns-enhancer');
            }
        } elseif ($key === 'date') {
            $new_columns[$key] = $value;
            $new_columns['last_edited'] = __('Last Edited', 'admin-post-columns-enhancer'); // Place Last Edited after Date
        } else {
            $new_columns[$key] = $value;
        }
    }

    return $new_columns;
}

/**
 * Format the Last Edited Date as "YYYY/MM/DD at H:i am/pm".
 */
function apce_format_last_edited_date($post_id) {
    $last_edit_timestamp = get_post_modified_time('U', false, $post_id, true);
    if (!$last_edit_timestamp) {
        return __('Never', 'admin-post-columns-enhancer');
    }

    // Format: YYYY/MM/DD at H:i am/pm
    $formatted_date = date('Y/m/d \a\t g:i a', $last_edit_timestamp);

    return sprintf(
        '%s<br>%s',
        __('Last Edited', 'admin-post-columns-enhancer'),
        esc_html($formatted_date)
    );
}

/**
 * Populate "Last Edited" and "Featured Image" columns.
 */
function apce_custom_column_content($column_name, $post_id) {
    if ($column_name === 'featured_image') {
        if (has_post_thumbnail($post_id)) {
            echo '<div style="height: 80px; overflow: hidden; display: flex; align-items: center; justify-content: center;">';
            echo get_the_post_thumbnail($post_id, 'medium', ['style' => 'height: 80px; width: auto; max-width: 100%;']);
            echo '</div>';
        } else {
            echo __('No Image', 'admin-post-columns-enhancer');
        }
    } elseif ($column_name === 'last_edited') {
        echo apce_format_last_edited_date($post_id);
    }
}

/**
 * Make "Last Edited" column sortable.
 */
function apce_make_last_edited_column_sortable($columns) {
    $columns['last_edited'] = 'modified';
    return $columns;
}

/**
 * Initialize hooks for all post types.
 */
function apce_init() {
    foreach (apce_get_all_post_types() as $post_type) {
        add_filter("manage_edit-{$post_type}_columns", function($columns) use ($post_type) {
            return apce_add_custom_columns($columns, $post_type);
        });
        add_action("manage_{$post_type}_posts_custom_column", 'apce_custom_column_content', 10, 2);
        add_filter("manage_edit-{$post_type}_sortable_columns", 'apce_make_last_edited_column_sortable');
    }
}
add_action('admin_init', 'apce_init');

How to Install the Plugin

  1. Create a new folder in your WordPress plugins directory:
    /wp-content/plugins/admin-post-columns-enhancer/
  2. Create a file inside this folder and name it:
    admin-post-columns-enhancer.php
  3. Paste the above code into this file.
  4. Activate the plugin from the WordPress admin panel (Plugins → Installed Plugins).
  5. Enjoy your new Featured Image and Last Edited columns!
Exit mobile version