avatar Deluxe Blog Tips About Projects

How to add custom columns to the admin post table in WordPress

The admin post table in WordPress shows you a list of posts with their details: title, author, categories, etc. These details give you a quick summary view of the posts. Sometimes, you might want to add custom columns to this page, such as 'Views', and 'Ratings',... to see more data about posts, especially when you use custom fields for posts. This article will show you how to do that.

Adding custom columns with Meta Box

Suppose that we're going to add a custom column 'Views' into the admin post table. In this article, I use the WP-PostViews plugin to add views to posts. This plugin adds a custom field with meta key views for storing the number of views. We will get the value from this custom field and show it in the admin post table.

PS: This is a simple guide to show custom fields in the admin post table, so you can do it yourself. If you're a Meta Box user, you can use its MB Admin Column extension to handle the task for you.

Now let's get started.

Register a custom column

First, we need to register a custom column for the admin post table. This is done by using a filter manage_{$post_type}_posts_columns. Here $post_type is the post type of the current screen, in this case we'll show for post. Paste the following code into your functions.php file of your theme:

add_filter( 'manage_post_posts_columns', function( $columns ) {
    $columns['views'] = 'Views';
    return $columns;
} );

The callback function takes an argument $columns which is an array of all registered columns. To add a new column, just add a new element to this array.

Note:

  • You might want to show all available columns using debugging functions, to see what column IDs are
  • In addition, you can unset an element to remove a column, or
  • You can reorder columns

If you want to add a column to a custom post type, replace post in the manage_{$post_type}_posts_columns filter.

Show the new column

To show the newly added column, we hook into the action manage_{$post_type}_posts_custom_column. Add the following code to your functions.php file:

add_action( 'manage_post_posts_custom_column', function( $name ) {
    if ( $name !== 'views' ) {
        echo get_post_meta( get_the_ID(), 'views', true );
    }
} );

The callback function takes one argument – the name of the column. So, we need to check this name, if it's views (which we have registered before), then we get the custom field value and show it.

If your new column needs more information, such as showing attachments, or post thumbnails, … you can use any WordPress function to get this data and show it. The example above just shows you the concept, I keep it simple to make it clear to follow.

If you want more advanced features, like making the column sortable, showing images or links, etc. please try MB Admin Column. It's a performant and powerful plugin to show custom fields made by Meta Box.

🔥 HOT: Interested in boosting your WordPress SEO? My Slim SEO plugin is a super lighweight and automated plugin that handles most the hard work for you: meta tags, sitemap, redirection, schema & link building.

👉 Have a look and you will love it: wpslimseo.com

Comments