In this tutorial I will explain how you can add a custom column to the users table in WordPress. I will go through an example of adding a custom field in the WP users table to display all post statuses and types at a glance.
I used to find myself often in the “Screen Options” area of the users table interface, unchecking some fields I didn’t want, plus trying to hunt down plugins to add fields that I did want.
These days though, I play with the functions.php file like she’s my best friend. Let me share some of my work in hopes to make your life simpler as well, even if just a little bit.
The WordPress Users Table Can Be Expanded on to Show More Post Related Data for Multi-author Sites
Let’s take the users table in the WordPress admin for example. Many people are running multi-author sites. For me… the default table data isn’t sufficient. I mean, it’s a good start, but sometimes I want to work with something that is a little more customized.
A lot of times I add unique data to the “profile edit” page and I want to expose that data in the users table. Other times, it is existing data that I want to pull in and see at a glance.
I have exposed information such as last logged in date and time, whether the user has added AdSense to their account for revenue sharing, among other things. Today though, I want to discuss something that I think just about anyone will find useful.
On one WordPress site that I administer we have the need to know what the writers are up to in a sense. We would like to see, at a glance, how many posts they have matching each status.
Let’s Go Through How to Display Post Related Data for Users for Any Post Type and Any Status
The code I offer below will work for any status, and any post type. So, even if you use custom post types and custom statuses (for example, if using the Edit Flow WordPress plugin), this will work for you.
So, rather than having a column for “Posts” with just one number in it, we can have one with several values. See the image below to get an idea of what I am referring to.
I also want to make it very simple to access a list of each of the posts. Fortunately that is very simple, and it just requires passing an author ID and post status to an existing WordPress admin URL so that we can filter by those criteria.
Now, how do we do it?
Rather than Remove Columns on an Admin by Admin Basis, Let’s Get Rid of the Default Columns with the WordPress “unset” Command
First, I want to remove the default posts column from the Users table. We could use the “Screen Options” area but lets make it a little more clean and “unset” it from the functions.php file. This way, if there are other admins, it will just “work” and we won’t have to explain to them the process of removing that column. They will simply login and the changes will be made, as if like magic.
Here’s the code below that you can add to the Theme Functions file:
add_action('manage_users_columns','kjl_modify_user_columns'); function kjl_modify_user_columns($column_headers) { unset($column_headers['posts']); return $column_headers; }
That’s it. Now, when you visit the Users area (/wp-admin/users.php) the default “Posts” column will be gonzo.
Oh, while we are at it, if you agree that the roles column is just taking up valuable real estate, you can “unset” that too by adding the following line to the above function. Just drop it before or after the posts unset command.
unset($column_headers['role']);
Adding New Columns to the User Table Is Just as Simple as Removing Them
Alright.. the next step is to add a new column. At first it won’t have any data, but we will get to that.
The column name will be unique but we can still title it “Posts”.
Let’s just add a line to the previous function. Put it just before the “return” statement. Here’s the line to add:
$columns['num_posts'] = 'Posts';
Here’s the complete code with the optional removal of the “role” column.
add_action('manage_users_columns','kjl_modify_user_columns'); function kjl_modify_user_columns($column_headers) { unset($column_headers['posts']); unset($column_headers['role']); $column_headers['num_posts'] = 'Posts'; return $column_headers; }
With CSSÂ You Can Modify the New Column Width – Here’s How
Sometimes WordPress will make the column too wide unnecessarily. It may not matter… it will depend a lot on how many columns you will be working with. If you find it needs to be smaller, then just add some CSS to the admin.
You can use the code below for that, if, when you are done… you feel the column width needs adjusting.
add_action('admin_head', 'kjl_custom_admin_css'); function kjl_custom_admin_css() { echo '<style> .column-num_posts {width: 8%} </style>'; }
Just modify the percentage to suit your needs.
We Are Now Ready to Create a Function to Query the WordPress Database and Return the Post Count That We Are Looking for
At this stage we can write a function that will return the post count based on post status, post type, and author id.
Here’s the code, short and sweet:
function kjl_count_user_posts_by_type_and_status( $userid, $post_status, $post_type = "post" ) { global $wpdb; $where = "WHERE post_author = " . $userid . " AND post_type = '" . $post_type . "' AND (post_status = '" . $post_status . "')"; $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); return apply_filters( 'get_usernumposts', $count, $userid ); }
This sexy bit of code returns a number based on the 3 parameters given. Post type will default to “post” if no value is given. So, if you aren’t working with custom post types then this will save you .3 seconds of your time.
Filling the New Custom Column with Relevant Data
Now, we want to add the code that will display the results that we are after in the new custom “Posts” column. This code will call the previously created function as many times as we need it to.
Here you go:
add_action('manage_users_custom_column', 'kjl_user_posts_count_column_content', 10, 3); function kjl_user_posts_count_column_content($value, $column_name, $user_id) { $user = get_userdata( $user_id ); if ( 'num_posts' == $column_name ) { $drafts = kjl_count_user_posts_by_type_and_status( $user_id, 'draft', 'post' ); $published = kjl_count_user_posts_by_type_and_status( $user_id, 'publish', 'post' ); $pending = kjl_count_user_posts_by_type_and_status( $user_id, 'pending', 'post' ); $all = $drafts + $published + $pending; return 'All: <a href="/wp-admin/edit.php?author='.$user_id.'" target="_blank">'.$all.'</a> Draft: <a href="/wp-admin/edit.php?post_status=draft&post_type=post&author='.$user_id.'" target="_blank">'.$drafts.'</a> Pending: <a href="/wp-admin/edit.php?post_status=pending&post_type=post&author='.$user_id.'" target="_blank">'.$pending.'</a> Published: <a href="/wp-admin/edit.php?post_status=publish&post_type=post&author='.$user_id.'" target="_blank">'.$published.'</a>'; } return $value; }
That’s all there is to it. You can add more to it if you have other statuses you want to display. Just make sure you create a variable to store the value, add that value to “all” and then echo that value on it’s own like shown in the HTML that is “returned” above.
I hope that you find this useful.
@Patricia, It should continue to work in the future.
Does this impact future wordpress updates?
Helped me out big time! Thanks for the post!
I find it very useful. Thank you 🙂