WordPress – Adding Custom Columns to Custom Post Listing

Sometimes, after creating your brand new custom post type in WordPress, with hundreds of meta values and taxonomies, you might want to add some of these meta values onto the page where the custom post type is being listed.

Let say that you have two new columns, “colour” and “engine-size” in your “car” custom post type. On admin page, when listing the cars, WordPress by default will only list the usual columns (title, date, author, etc) but it will be beneficial to have the colour and engine-size columns as well (so that you don’t have to open the single car in order to view the values of colour and engine size).

Luckily, WordPress offers you an easy way to do this.

There are two hooks that you will have to use, one to define the new column headers (the labels – in this example they will be “Colour” and “Engine Size”) and the other one for printing the value of the column for a given car.

See the following code:

function xx_get_headers($defaults) 
    $defaults['colour'] = 'Colour'; 
    $defaults['engine-size'] = 'Engine Size'; 
function xx_get_meta_value_by_post_id($column_name, $post_ID) 
    //assuming that your post meta values are the same as the column ids
    echo get_post_meta($post_ID, $column_name, true);
add_filter('manage_car_posts_columns', 'xx_get_headers', 10);
add_action('manage_car_posts_custom_column', 'xx_get_meta_value_by_post_id', 10, 2);
This entry was posted in Blog. Bookmark the permalink.

Comments are closed.