1. Posts Table Pro
  2. Developer documentation

Adding a custom column to the posts table

These instructions will explain how to add a custom column in Posts Table Pro. Custom columns are a good way to display data programmatically which is not available as one of the standard table columns.

Need professional assistance?
We've partnered with Codeable to provide our customers with expert help if required.

If you would prefer a simpler option that doesn't require custom coding, then you could create a custom field and display this as a column instead.


To add a custom column to your table you need to do the following:

  1. Add your column to the columns used in the posts table. This can be via the plugin settings page, or directly in the shortcode using the columns option. The column can be called anything you like, e.g. sale_price, event_organiser, media_type, etc. It doesn't matter what the name of the column is as long as you don't use an existing column name already provided by the plugin.
  2. Create a new class which implements the Table_Data_Interface interface. This is the class that will retrieve the actual data for your custom column.
  3. Add a hook to your theme or custom plugin which returns an instance of the class created in Step 2.

Worked example

For this example we're going to assume the custom column we're adding is called media_type.

1. Adding the column to the options or shortcode

In this example, we're going to add the column directly in our shortcode. So our shortcode will be:

[posts_table columns="title,categories,author,media_type"]

2. Creating the data class

Each column in the table has an associated object which implements Table_Data_Interface. The interface has 3 functions, but the main one that you'll be implementing is the get_data() function.

To simplify creating this class, you can extend the Abstract_Table_Data class. This provides base implementations for the other two functions which you probably won't need.

Here's an example of a data class for our media_type column:

use Barn2\Plugin\Posts_Table_Pro\Data\Abstract_Table_Data;

if ( ! class_exists( 'Barn2\Plugin\Posts_Table_Pro\Data\Abstract_Table_Data' ) ) {
    return;
}

/** 
 * Gets data for the 'media_type' column to use in the posts table. 
 */ 
class Posts_Table_Media_Type_Column extends Abstract_Table_Data { 

    public function get_data() { 
        // Retrieve the media type from somewhere. In this example, we get it from the post meta. 
        $data = get_post_meta( $this->post->ID, '_media_type', true ); 
        return $data;
    } 
}

3. Adding the data class hook

The hook you need to add is posts_table_custom_table_data_<column>. So for this example we would register this hook:

add_filter( 'posts_table_custom_table_data_media_type', function( $obj, $post, $args ) { 
    return new Posts_Table_Media_Type_Column( $post ); 
}, 10, 3 );

This filter accepts 3 arguments: the data object to return (defaults to false), the current post object (WP_Post) and the table arguments object (Table_Args).

You could put the callback and the data class in the same file if you want, or keep them separate. Once you've added your callback and data object, you should now see your custom column in the table.

Setting the custom column heading

Posts Table Pro will give your column a heading based on its name. So in our example for media_type, the heading would be "Media Type". If you want a different column heading, you can do this in the usual way by adding a : (colon) after the column followed by the heading. E.g:

[posts_table columns="title,categories,author,media_type:File Type"]

A note on caching

If the data for your custom column returns unexpected or stale data, please check your caching settings as you may need to disable or flush the cache.

Related Articles

Still need help?
If searching the knowledge base hasn't answered your question, please contact support.