Paulund
2013-04-08 #wordpress

Add Custom Post Meta Data To Post List Table

One of the best thing about WordPress is that you can customise almost anything. In the admin area you can see a list of all the posts you have added in WordPress. Within this table it shows the basic information for each of the posts, the title, the author, the category, tags, comments and the date the post was published. WordPress has a number of different filters and actions that allow you to edit the output of the column so you can add your own data to this list. For example if you have custom post meta data which is useful information you want to display on the list of posts you can add new custom columns to the list. In this article you will learn how to add new columns to the post list, how you can add data to the column and how you can make this column sortable.

Add New Columns

First we start off by adding the new column to the list, for this we use the WordPress filter manage_edit-post_columns. This will allow you to edit the output of the columns by adding new values to the column array. The callback function on this filter will pass in one parameter which are the current columns on the list, the return of this function will be the new columns on the post table. This means that we can add additional values to the array to add extra columns to the table. The following code will add a new column to the table just after the title column.

// Add a column to the edit post list
add_filter( 'manage_edit-post_columns', 'add_new_columns');

/**
* Add new columns to the post table
*
* @param Array $columns - Current columns on the list post
*/
function add_new_columns( $columns ) {
$column_meta = array( 'meta' => 'Custom Column' );
$columns = array_slice( $columns, 0, 2, true ) + $column_meta + array_slice( $columns, 2, NULL, true );
return $columns;
}

Add Columns To Custom Post Types

If you have custom post types in your site and want to add additional columns to this list, WordPress comes with built in filters you can apply to add new columns to this table.


add_filter( 'manage_${post_type}_posts_columns', 'add_new_columns');

If you have a custom post type of portfolio then you can use the following code to add a column to the list of portfolio post types.

function add_portfolio_columns($columns) {
return array_merge($columns,
array('client' => __('Client'),
'project_date' =>__( 'Project Date')));
}
add_filter('manage_portfolio_posts_columns' , 'add_portfolio_columns');

Add Data To Custom Columns

Once you have created the new columns for the posts list you can now add data to the new columns by using the WordPress action manage_posts_custom_column. Adding an action to this will be called on each column, from this call we can get data for the post display this on the post list. The following code will check what column we are on and get the custom post meta data for the current post and display this in the column.

// Add action to the manage post column to display the data
add_action( 'manage_posts_custom_column' , 'custom_columns' );

/**
* Display data in new columns
*
* @param $column Current column
*
* @return Data for the column
*/
function custom_columns( $column ) {
global $post;

switch ( $column ) {
case 'meta':
$metaData = get_post_meta( $post->ID, 'twitter_url', true );

echo $metaData;
break;
}
}

Add Data To Custom Post Type Columns

Along with being able to add a filter to custom post types by adding new columns, WordPress has a built in action you can use to add data to custom columns. In the above example we add a new column just for post types of a portfolio to add two new columns to the list, using the below code you can add data to these new columns.


function custom_portfolio_column( $column, $post_id ) {
switch ( $column ) {
case 'project_date':
echo get_post_meta( $post_id , 'project_date' , true );
break;

case 'client':
echo get_post_meta( $post_id , 'client' , true );
break;
}
}
add_action( 'manage_portfolio_posts_custom_column' , 'custom_portfolio_column', 10, 2 );

Make Columns Sortable

By default the new custom columns are not sortable so this makes it hard to find data that you need. To sort the custom columns WordPress has another filter manage_edit-post_sortable_columns you can use to assign which columns are sortable. When this action is ran the function will pass in a parameter of all the columns which are currently sortable, by adding your new custom columns to this list will now make these columns sortable. The value you give this will be used in the URL so WordPress understands which column to order by. The following to allow you to sort by the custom column meta.


// Register the column as sortable
function register_sortable_columns( $columns ) {
$columns['meta'] = 'Custom Column';

return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'register_sortable_columns' );

That's all the information you need to change the way posts are listed in your admin area. What useful information do you wish was displayed in the post list?