Using WordPress ‘manage_posts_custom_column’ PHP action

The manage_posts_custom_column WordPress PHP action allows you to manage custom columns content in the Posts list table for non-hierarchical post types, such as posts.

Usage

add_action('manage_posts_custom_column', 'your_custom_function', 10, 2);

function your_custom_function($column_name, $post_id) {
  // your custom code here
}

Parameters

  • $column_name (string) – The name of the column to display.
  • $post_id (int) – The current post ID.

More information

See WordPress Developer Resources: manage_posts_custom_column

Examples

Add a custom ‘Views’ column

Display post views count in a custom ‘Views’ column:

// Add 'Views' column to Posts list table
add_filter('manage_posts_columns', 'add_views_column');
function add_views_column($columns) {
  $columns['views'] = 'Views';
  return $columns;
}

// Populate 'Views' column with post views count
add_action('manage_posts_custom_column', 'display_views_column', 10, 2);
function display_views_column($column_name, $post_id) {
  if ('views' === $column_name) {
    $views = get_post_meta($post_id, 'views', true);
    echo $views;
  }
}

Add a custom ‘Word Count’ column

Display the word count of post content in a custom ‘Word Count’ column:

// Add 'Word Count' column to Posts list table
add_filter('manage_posts_columns', 'add_word_count_column');
function add_word_count_column($columns) {
  $columns['word_count'] = 'Word Count';
  return $columns;
}

// Populate 'Word Count' column with post content word count
add_action('manage_posts_custom_column', 'display_word_count_column', 10, 2);
function display_word_count_column($column_name, $post_id) {
  if ('word_count' === $column_name) {
    $content = get_post_field('post_content', $post_id);
    $word_count = str_word_count(strip_tags($content));
    echo $word_count;
  }
}

Display the featured image thumbnail in a custom ‘Featured Image’ column:

// Add 'Featured Image' column to Posts list table
add_filter('manage_posts_columns', 'add_featured_image_column');
function add_featured_image_column($columns) {
  $columns['featured_image'] = 'Featured Image';
  return $columns;
}

// Populate 'Featured Image' column with post featured image thumbnail
add_action('manage_posts_custom_column', 'display_featured_image_column', 10, 2);
function display_featured_image_column($column_name, $post_id) {
  if ('featured_image' === $column_name) {
    $thumbnail = get_the_post_thumbnail($post_id, 'thumbnail');
    echo $thumbnail;
  }
}