The post manage page in WordPress shows you a list of posts with their details: title, author, categories, etc. These details give blog manager a quick summary view of the posts. Sometimes, you might want to add new custom column to this page, such as ‘Views’, ‘Ratings’ to see more statistics about posts, especially when you have installed the corresponding plugins. This article will show you how to do that.

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

Register custom column

First, we need to register a custom column to WordPress. This is done by using a filter manage_posts_columns as in the following code (paste it into your functions.php file):

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

The function my_columns take 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 print_r() or var_dump() functions. In addition, you can unset an element to remove a column or reorder columns.

This filter works fine, but it will add a new custom column to all post manage page, including for custom post type. So, to specify the manage page for a custom post type, we use another filter: manage_edit-customposttype_columns, where customposttype is the slug of you custom post type. For example, if you want to add a new column to manage page for post type film, just use:

add_filter('manage_edit-film_columns', 'my_columns');
function my_columns($columns) {
    $columns['views'] = 'Views';
    return $columns;
}

Show the new column

To show the new added column, we hook into the action manage_posts_custom_column. Add the following code into your functions.php file:

add_action('manage_posts_custom_column',  'my_show_columns');
function my_show_columns($name) {
    global $post;
    switch ($name) {
        case 'views':
            $views = get_post_meta($post->ID, 'views', true);
            echo $views;
    }
}

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

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

Hope this tutorial is helpful for you.

20 Comments

  1. Great tutorial. There is also a free plugin called Admin Columns that allows you to create these columns with an interface and without any code :)

    The pro version allows you to sort and filter on those columns and even use inline edit for the values in the columns. It also goes well with columns programmed like in this tutorial.

    https://www.admincolumns.com/

    1. In-case you didn’t notice this is a tutorial for programmers that are making a plugin, not the end user that wants a plugin to solve everything. I’m absolutely sick of seeing those BS “oh get a plugin for that” kind of answers, when we’re the ones making a plugin and don’t need another.

    1. That’s strange because there is no difference between CPT non-hierarchical and hierarchical, all hooks are the same and the code should work. How do you implement that?

  2. Thanks, I have one question tho. How to change the order of the columns? Because now the date column is first and my custom columns are after that. I want my custom columns first and the date column as last one (which is the default).

    1. Sorting is a little bit tricky, it depends on the data displayed and post query. I’ll probably post about that. It’s hard to say in comments, because it’s quite long.

  3. I have stuck here, I use met-box and removed default title field and the WYSIWYG editor.
    I want to re-use the title column and show a field from my meta-box instead of text “Auto Draft

    Or is there a way i can add my column and show the little popup menu which allows us to edit the post.

    Regards,

  4. Will it be possible to add another column correspondent to a special added field in the article editting process which would be shown in both Front and Backend? Like a price tag, let’s say. I want to add articles in a custom display, each article showing a price. And I wouldn’t open the article (FE or BE) to see the price. I want to see it on the list. But also be able to change it whitin the article content in BE.

    1. I don’t think that makes sense, because the column is designed for list of post, while the added field (custom field) belongs to one post only.

  5. I putted the code in tags but he still rendered the a href.
    This is the part of the code fixed
    $columns['Featured'] = 'Featured';
    (Remove // )

  6. Thanks! Finally a tutorial about custom post columns that actually works. I’m with the other commentators who would like a tutorial about how to allow filtering and sorting by this column.

    1. After wanting to do the same thing and 2 minutes of trial and error I figured out a way, probably not the most correct one but it works..

      I was trying to add a featured column to show the featured posts. Basically you create the column name in an anchor link with orderby=columnname and order=asc parameters.


      add_filter('manage_posts_columns', 'my_columns');
      function my_columns($columns) {
      $columns['Featured'] = 'Featured';
      return $columns;
      }

      add_action('manage_posts_custom_column', 'my_show_columns');
      function my_show_columns($name) {
      global $post;
      switch ($name) {
      case 'Featured':
      $feat = get_post_meta($post->ID, '_cf_feat_enabled', true);
      if ($feat == 1) $feat = 'YES';
      if ($feat == '0') $feat = '';
      echo $feat;
      }
      }

      The only downside is that it will not switch between ASC and DESC. But all I want to show is the featured on the top if someone clicks on it. So it works fine for that..

    2. It keeps removing the href tag. Let me try again:
      a href /=/ “?orderby=featured&order=asc”>Featured /a>;

      Hi Tran, can you update my original post and replace the third line of code with the above ?

      Your blog keeps filtering out my attempt to paste a href without making the link.

Leave a Reply

Your email address will not be published. Required fields are marked *