Add a Custom Column to Posts Table in WordPress (Display Featured Image & Post Meta)

WordPress shows only a few columns in the post list table to display the post data, like the title, author, and date of a post. But it also makes it possible to add more columns to show additional associated data in the posts table. Learn how to add a custom column to the posts table in WordPress without using a plugin.

Add a Custom Column to Posts Table in WordPress

WordPress comes with useful action and filter hooks that can be used to extend the functionality of a theme. Some of these hooks make it possible to add columns to taxonomy table or to any post type table to display associated term/post metadata. For example, if we have a "Property" post type that has a featured image and custom post meta associated with it, we can show the image and post meta as separate columns in the posts table. We will demonstrate in this post how to customize posts table columns using different hooks.

 

WordPress Hooks to Manage Posts Table Columns

There are two hooks that WordPress offers to manage posts table columns. A filter hook is used to register a new column, and an action hook is used to add content to that column.

add_filter('manage_{$post_type}_posts_columns', string[] $post_columns);
do_action('manage_{$post_type}_posts_custom_column', string $column_name, int $post_id);

The "manage_{$post_type}_posts_columns" is for registering our new columns, while "manage_{$post_type}_posts_custom_column" is for showing the content of those columns, and $post_type is a slug of the post type we want to add these columns for.

add_filter('manage_{$post_type}_posts_columns', string[] $post_columns);
  • $post_columns: (array) - Associative array of strings for column headings.
do_action('manage_{$post_type}_posts_custom_column', string $column_name, int $post_id);
  • $column_name: (string) - The name of the column for which we are adding the content.
  • $post_id: (int) - The ID of the current post.
 

How to Add a Column to WordPress Posts Table?

Additional columns can be easily added to the posts table by using the mentioned hooks. We create a separate property-columns.php file that utilizes these hooks, then we include this file in the theme's functions.php file. We will add a column for the featured image and another one for the property price post meta. We will also utilize the wp_add_inline_style function to add inline styles specific to the posts table page only.

  • Register a filter hook manage_{$post_type}_posts_columns for column headings.
  • Add two columns to the $columns array.
  • Sort the columns array to change the position of columns in the table.
  • Register an action hook manage_{$post_type}_posts_custom_column to show the content of each column.
  • Add a switch statement to check the column and add content accordingly.

property-columns.php

<?php
add_action('admin_enqueue_scripts', 'admin_scripts_and_styles');
function admin_scripts_and_styles() {
global $pagenow;
$post_type = isset($_GET['post_type']) ? sanitize_text_field($_GET['post_type']) : null;

if($pagenow === 'edit.php' && $post_type === 'property'){
wp_register_style( 'post-columns-style', false );
wp_enqueue_style( 'post-columns-style' );
$custom_css = "
.post-type-property th.column-featured_image{
width: 70px;
}
.post-type-property td.column-featured_image > img{
max-width: 100%;
height: auto;
}";
wp_add_inline_style( 'post-columns-style', $custom_css );
}
}

add_filter('manage_property_posts_columns', 'register_property_columns' );
function register_property_columns($columns)
{
$sorted_columns = [];
// Get array of column keys for sorting
$column_keys = array_keys($columns);

// Add featured image column to second position in columns
array_splice($column_keys, 1, 0, ['featured_image']);

// Add price column to second last position in columns
array_splice($column_keys, count($column_keys) - 1, 0, ['property_price']);

// Add both columns to original columns at the end
$columns['featured_image'] = __('Image', 'text-domain');
$columns['property_price'] = __('Price', 'text-domain');

// Loop through keys and prepare new sorted array of columns
foreach($column_keys as $column_key){
$sorted_columns[$column_key] = $columns[$column_key];
}

return $sorted_columns;
}

add_action('manage_property_posts_custom_column', 'add_property_columns_content', 10, 2);
function add_property_columns_content($column, $post_id)
{
switch ($column) {
case 'featured_image':
if (has_post_thumbnail()) {
the_post_thumbnail('thumbnail', ['class' => 'attachment-thumbnail attachment-thumbnail-small']);
}
break;

case 'property_price':
echo esc_html(get_post_meta($post_id, 'property_price', true));
break;
}
}

After adding the above code to property-columns.php, we need to include it in our functions.php for changes to take effect.

include('property-columns.php');

The custom columns we added will start to display in the property post type table as in the screenshot below:

wordpress-add-custom-column-posts-list-table-01

These columns will also be displayed in Screen Options as in the screenshot:

wordpress-add-custom-column-posts-list-table-02

Adding columns to the posts table is a simple way to display post-related data in separate columns. We demonstrated how to add columns to the posts table of any post type by utilizing just two hooks. The code can be used to add any post data to any post type table, and further formatting can be added to display different types of data like dates, prices, units, etc.