Each item in the
admin_cols array is either a string name of an existing column, or an associative
array of information for a custom column.
Defining a custom column is easy. Just define an array which includes the column title, column
type, and optional callback function. You can display columns for post meta, taxonomy terms,
post fields, the featured image, and custom functions.
The example below adds two columns; one which displays the value of the post's
event_type meta
key and one which lists the post's terms from the
location taxonomy:
register_extended_post_type( 'event', array(
'admin_cols' => array(
'event_type' => array(
'title' => 'Event Type',
'meta_key' => 'event_type'
),
'event_location' => array(
'title' => 'Location',
'taxonomy' => 'location'
)
)
) );
That's all you need to do. The columns will handle all the sorting and safely outputting the data
(escaping text, and comma-separating taxonomy terms). No more messing about with all of those
annoyingly named column filters and actions.
Each item in the
admin_cols array must contain one of the following elements which defines the column type:
- taxonomy - The name of a taxonomy
- meta_key - A post meta key
- post_field - The name of a post field (eg. post_excerpt)
- featured_image - A featured image size (eg. thumbnail)
- connection - A connection ID registered with the Posts 2 Posts plugin
- function - The name of a callback function
The value for the corresponding taxonomy terms, post meta or post field are safely escaped and output
into the column, and the values are used to provide the sortable functionality for the column. For
featured images, the post's featured image of that size will be displayed if there is one.
There are a few optional elements:
- title - Generated from the field if not specified.
- function - The name of a callback function for the column (eg.
my_function) which gets called
instead of the built-in function for handling that column. Note that it's not passed any parameters,
so it must use the global $post object.
- default - Specifies that the admin screen should be sorted by this column by default (instead of
sorting by post date). Value should be one of
asc or
desc to control the default order.
- width & height - These are only used for the
featured_image column type and allow you to set an
explicit width and/or height on the
![]()
tag. Handy for downsizing the image.
- field & value - These are used for the
connection column type and allow you to specify a
connection meta field and value from the fields argument of the connection type.
- date_format - This is used with the
meta_key column type. The value of the meta field will be
treated as a timestamp if this is present. Unix and MySQL format timestamps are supported in the
meta value. Pass in boolean true to format the date according to the 'Date Format' setting, or pass
in a valid date formatting string (eg.
d/m/Y H:i:s).
- cap - A capability required in order for this column to be displayed to the current user. Defaults
to null, meaning the column is shown to all users.
- sortable - A boolean value which specifies whether the column should be sortable. Defaults to true.