How to Add Custom Columns in Posts Screen in WordPress

The posts screen displays Title, Author, Categories, Date, Comments and Tags by default. But we need not be limited to these columns only.

If we have one or more custom fields associated with posts then you can display that on this screen easily using WordPress inbuilt hooks and filters.

Suppose we have a custom field associated with posts called post_is_premium (imaginary). This field shows whether a post is a premium post or not.

There is another custom field, say content_style_type (again, imaginary field) that shows the content writing style out of professional, business or friendly.

To display these column in posts screen we will need a filter and a hook.

The filter we will use is manage_post_posts_columns.

add_filter('manage_post_posts_columns', 'xyz_posts_columns_cb');

Here xyz_posts_columns_cb is the callback function for this filter but the function name can be anything you want.

What this filter does is – it passes the entire array of columns that will be displayed on the posts screen.

function xyz_posts_columns_cb($columns) {
    // do your stuff
    return $columns;
}

We will use the callback function to add the custom columns of our choice!

function xyz_posts_columns_cb($columns) {
    $columns['post_is_premium'] = "Premium Post";
    $columns['content_style_type'] = "Content Style";
    return $columns;
}

We are adding two custom columns post_is_premium and content_style_type with heading Premium Post and Content Style respectively. The heading will show up along with other headers in the posts screen.

Next, we need an action hook to produce the values for these columns.

 add_action('manage_post_posts_custom_column' , 'xyz_custom_column_values', 10, 2);

The action hook above, manage_post_posts_custom_column is used to create the column values with a callback function xyz_custom_column_values (name can be anything you want).

function xyz_custom_column_values($column, $post_id) {
    switch( $column ) {
         case 'post_is_premium': 
             echo "Yes";
         break;
         case 'content_style_type': 
             echo "Professional";
         break;
    }
}

The callback function takes two parameters, $column and $post_id.

Here $column is the current custom column passed to the callback function and $post_id is the id of the post associated with the column.

The final output looks like this –

We are printing out static values for the demo, but in practice these values will be dynamic as per the site’s requirements.