The posts list and management page in WordPress is where you see the list of posts, pages, etc. along with some basic info: the post’s title, author, categories, etc. These details give blog managers a quick overview of the posts and lets them manage them (add, edit, trash, etc.). Under normal circumstances, this information is enough to locate and manage posts.

However, you might want to add new information to this page, such as related posts (when using custom posts this can be very handy), statistics, etc. This article will show you how to do that and a few more tricks.

Let’s say you want to know the number of words in each WordPress post without having to click on every single one on them. Being able to see that piece of information on the post list page could be very useful, wouldn’t it? Now, here’s how.

First things first! Before editing your theme files, make sure you have a backup in a safe place. Just to avoid any unpleasant surprises.

Count the words

In order to know how many words are in a given post, you can use str_word_count() function. This function takes a string and returns the number of words in it. We can apply that function to the post content and get the word count. However, as posts contain HTML tags, we need to strip them out to get a more accurate result. For that, we use WordPress’ strip_shortcodes() and PHP’s strip_tags(). For that, we can develop a custom function that takes a post’s ID and returns the number of words in the post (you should paste it on your theme’s functions.php file).

Adding the custom column to WordPress

Now that we know how to count words, we need to add a column to the posts list page. To accomplish that, we must register a new column by creating a ‘filter’ and an ‘action’ to register and then display the new column’s info. If you are wondering about the difference between WordPress actions and filters, you can read about them here. The full code (including the word-count function) would be like this:

This first function is the word-count function. The second part of the code creates a filter for the manage_posts_columns, a filter applied to the columns shown on the manage posts screen. We use it to add a column named “wordcount” and then return the updated column array.

The last piece of code is an action triggered when you are on the posts management page and columns are displayed. Here’s where we actually display the word count for the post. Because this action is triggered for every column in each post, we need to check that we’re actually on the right column before displaying anything. Otherwise, it would show the word-count in each column, messing up the page’s content.

That’s it! We now have the word-count for each post on the blog.

Reordering columns

Adding a column like this would add the column at the end of the line, which in some cases might not be what’s required. In that case, what can be done is to re-create the columns list and insert the new column where we need it. For instance, let’s suppose we want the word-count column after the title column. In that case, the filter function would change to (the action function remains unchanged).

Summary
Adding custom data to WordPress Page Post List
Article Name
Adding custom data to WordPress Page Post List
Description
WordPress post list can be enhanced by adding new data columns and display additional information about the post. In this article, you will see how.
Author
Publisher Name
Ivan Melgrati
Publisher Logo