Adding Custom Columns to WordPress Post listing

July 13th, 2011 @ 9am : 4 comments : Socialize This
Filed Under: Tags: , , , ,

In a reader comment one of my previous posts about “Adding columns to Taxonomy tables”. The commenter asks how to add columns to a normal Posts listing.

Well, readers here is the simple solutions. There are basically 2 hooks. One is a filter. The other an action. Below are examples of how to setup these hooks. I explain the details below the code listing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
add_filter( 'manage_edit-post_columns', 'admin_post_header_columns', 10, 1);
add_action( 'manage_posts_custom_column', 'admin_post_data_row', 10, 2);

function admin_post_header_columns($columns)
{
    if (!isset($columns['note']))
        $columns['note'] = "Notes";
   
    return $columns;
}

function admin_post_data_row($column_name, $post_id)
{
    switch($column_name)
    {
        case 'note':       
            // Logic to display post 'Note' field information here.
            // $post_note = get_post_meta($post_id, 'note', true);
            //if ($post_note)   echo $post_note;
           
            break;
                       
        default:
            break;
    }
}

So what are these filters/actions function?

The first line, add_filter( ‘manage_edit-post_columns’… sets up a filter. This filter adds the new column to the listing. This filter is called only once when the page is loaded. Since this is a Filter WordPress will call our function ‘admin_post_header_columns’ and pass in a parameter. In this case the parameter is an array of columns. As part of our function we can change/add/delete items from the array then we MUST return the array. In our simple example all we are doing is adding an item to the array for our ‘Notes’ columns. As with any PHP array we assign a key to be ‘note’ and the column label ‘Notes’. Then return the array back to WP.

A note about the filter name. Notice we setup the filter as using the first parameter ‘manage_edit-post_columns’. Keep in mind this filter is specific to Posts. If you wanted to do this same thing for Pages you would instead use ‘manage_pages_columns’. Or if you were using a custom post type the value would be ‘manage_{$post_type}_posts_columns’ where ‘{$post_type}’ is your post_type. For example if we register a new post_type Products with the key ‘products’ then the filter would use the parameter of ‘manage_products_posts_columns’. On to the second hook. The action.

The second line of our code registers an action. While the previous filter added the column to the listing. This action gets called for each row of the displayed posts to display the contents in the row cell for that column. In WordPress an action is different than a filter in that your function may be passed some optional parameters but you are not expected to return any values. For our purpose we setup an action to the hook ‘manage_posts_custom_column’ which will call our function ‘admin_post_data_row’.

Inside our function we setup a case statement to switch on the column name. In our instance we only want to effect the ‘note’ column. This ‘note’ value is the array ‘key’ we defined in our previous filter function.

Back to the action hook registration. Notice the third and fourth parameters on this action registration call. The third parameter tells WordPress the priority of this hook o when to process it in relation to other hooks. A lower number means sooner a higher number means later. We set this to ’10’ which is a default value. The fourth parameter tells WordPress how many arguments to pass to our function. By default there will be only one argument, the column name. But since each row (post item) will have different values we need the second parameter which in this case is the ID of the post so we can look up the related custom field.

Also, like the filter hook where we setup the column there are different filters to call based on if you are using this on Pages, Posts or some custom post_type. In our case we are adding the column to the Posts listing so the first parameter of our hook is ‘manage_posts_custom_column’. If we were adding a column to Pages the parameter would instead be ‘manage_pages_custom_column’. And if we were adding this column to a custom post_type the parameter would be ‘manage_{$post->post_type}_posts_custom_column’ where ‘{$post->post_type}’ is the key to our registered post_type.

I hope this helps with a deeper understanding of WordPress and an appreciation of the wonderful world of WordPress actions and filters.

Both comments and pings are currently closed.

4 Responses to “Adding Custom Columns to WordPress Post listing”

  1. Owen Says:

    Hi,

    Thanks so much for the tutorial. If you get a chance can you help me with my code? I’ve succeeded in adding the column to a custom post type, but the meta value does not appear. Here’s the code:

    add_filter( ‘manage_person_posts_columns’, ‘admin_post_header_columns’, 10, 1);
    add_action( ‘manage_person_posts_custom_column’, ‘admin_post_data_row’, 10, 2);

    function admin_post_header_columns($columns)
    {
    if (!isset($columns[‘firstname’]))
    $columns[‘n’] = “First Name”;

    return $columns;
    }

    function admin_post_data_row($column_name, $post_id)
    {
    switch($column_name)
    {
    case ‘firstname':
    // Logic to display post meta field information here.
    $person_name_first = get_post_meta($post_id, ‘person_name_first’, true);
    if ($person_name_first) echo $person_name_first;

    break;

    default:
    break;
    }
    }

    Thanks!

  2. Razvan Says:

    Great stuff! Thanks!
    your code adds the column as the last column.
    If you want to put it in between you should do this:
    function admin_post_header_columns($columns)
    {
    $new_columns = array();
    if(isset($columns[‘cb’]))$new_columns[‘cb’] = ”;
    if(isset($columns[‘title’])) $new_columns[‘title’] = ”;
    if(isset($columns[‘author’])) $new_columns[‘author’] = ”;
    if(isset($columns[‘categories’])) $new_columns[‘categories’] = ”;
    if(isset($columns[‘tags’])) $new_columns[‘tags’] = ”;
    if (!isset($columns[‘note’])) $new_columns[‘note’] = “Eurobox Category”;
    return array_merge($new_columns, $columns);

  3. Razvan Says:

    Sorry, pressed Enter too soon :D
    Here’s the complete code:
    function admin_post_header_columns($columns)
    {
    $new_columns = array();
    if(isset($columns[‘cb’])) $new_columns[‘cb’] = ”;
    if(isset($columns[‘title’])) $new_columns[‘title’] = ”;
    if(isset($columns[‘author’])) $new_columns[‘author’] = ”;
    if(isset($columns[‘categories’])) $new_columns[‘categories’] = ”;
    if(isset($columns[‘tags’])) $new_columns[‘tags’] = ”;
    if (!isset($columns[‘note’])) $new_columns[‘note’] = “Notes”;
    return array_merge($new_columns, $columns);
    }

  4. omer Says:

    Thanks for the code.
    Just one question, how do I insert a text that will appear under the “note” column for all the posts?