Add a Custom Column to the WordPress Posts List

You can add your own column to the list of posts in the dashboard. This example adds a Words column with the number of words of each post. It needs two hooks: one to add the column title, and one to print the value for each row.

<?php
// 1. Add the column after the title
add_filter( 'manage_posts_columns', function ( $columns ) {
	$new = array();
	foreach ( $columns as $key => $label ) {
		$new[ $key ] = $label;
		if ( 'title' === $key ) {
			$new['tc_words'] = 'Words';
		}
	}
	return $new;
} );

// 2. Print the value of each row
add_action( 'manage_posts_custom_column', function ( $column, $post_id ) {
	if ( 'tc_words' === $column ) {
		echo (int) str_word_count( wp_strip_all_tags( get_post_field( 'post_content', $post_id ) ) );
	}
}, 10, 2 );

Test it

Terminal output

$ curl -s -b cookies.txt "http://example.com/wp-admin/edit.php" | grep -o -E 'data-colname="Words">[0-9]+' | sed 's/.*>//'
5
15
$ wp eval 'echo str_word_count( "One two three four five" );'
5

The list shows the values of the new column: 5 for our new post and 15 for the sample post “Hello world!”. To make the column sortable, add the filter manage_edit-post_sortable_columns and handle the order in pre_get_posts. For a custom post type use manage_{post_type}_posts_columns.


Learn more about the similar topics:
Tutorials
WordPress Posts
WordPress Pages
WordPress Block Editor (Gutenberg)
WordPress Categories and Tags
WordPress Media Library
WordPress Comments
Exercises & Assignments
Add categories, a child category and a tag to a post
Interview Questions & Answers
How to limit WordPress post content length?
What is the difference between Posts & Pages in WordPress?
Which Gutenberg blocks support wide and full width alignment?
What is Post Status in WordPress?
What is the difference between categories and tags in WordPress?
What is Gutenberg in WordPress?