$post_columns is an associative array of “column name” ⇒ “label”. The “column name” is passed to callback functions to identify the column. The “label” is shown as the column header.
Built-in Column Types:
Note: Listed in order of appearance. By default, all columns supported by the post type are shown.
title: Post title. Includes “edit”, “quick edit”, “trash” and “view” links. If $mode (set from $_REQUEST[‘mode’]) is ‘excerpt’, a post excerpt is included between the title and links.
I decided to use tags as a column name (key on the array) and spent a few minutes wondering why no value was appearing for the column. The function to pull out the data wasn’t even called. It is of course, a default field on the post type and the fuction won’t get called WP uses its default value.
Helper function to insert an element after a specific array key
/**
* It takes an array, a specific key, and a new element, and inserts the new element after the
* specific key
*
* @param array arr The array where you want to insert the new element.
* @param string specific_key The key of the array element you want to insert after.
* @param array new_element The new element to be inserted.
*
* @return array
*/
function wpdocs_insert_element_after_specific_array_key( $arr, $specific_key, $new_element ) {
if ( ! is_array( $arr ) || ! is_array( $new_element ) ) {
return $arr;
}
if ( ! array_key_exists( $specific_key, $arr ) ) {
return $arr;
}
$array_keys = array_keys( $arr );
$start = (int) array_search( $specific_key, $array_keys, true ) + 1; // Offset
$spliced_arr = array_splice( $arr, $start );
$new_element_key = $new_element['key'];
$arr[ $new_element_key ] = $new_element['value'];
$new_arr = array_merge( $arr, $spliced_arr );
return $new_arr;
}
If you’re having trouble finding out how to find the name of your custom taxonomy column so you can reorder your columns with custom taxonomies like I was, the index in your $columns array is $columns[taxonomy-{taxonomy_name}].
For instance I had made a ‘Document Category’ custom taxonomy for my ‘Document’ Custom Post Type, so that index for me was $columns[taxonomy-document_category]. So the entire code to order my custom taxonomy with my new ‘Attachment Type’ column was:
For further management of columns, check:
https://developer.wordpress.org/reference/hooks/manage_post-post_type_posts_custom_column/
To set the custom column values
https://developer.wordpress.org/reference/hooks/list_table_primary_column/
To set the primary (default) column
Example migrated from Codex:
Add Columns
Suppose you have a ‘books’ custom post type and you want to add the publisher and book author in the edit page but remove the post author.
To reorder columns
Just use the
$custom_col_order
array to reorder the columns in{$post_type}
.NOTE: in the above example,
item
is the custom post typeBe careful with your choice of column name.
I decided to use
tags
as a column name (key on the array) and spent a few minutes wondering why no value was appearing for the column. The function to pull out the data wasn’t even called. It is of course, a default field on the post type and the fuction won’t get called WP uses its default value.Prefixing the column fixed the issue.
Example migrated from Codex:
Replace Columns
Here’s another example that completely replaces the columns, rather than adding and removing specific ones.
Add a new column after an specific column. For example-
The code below adds a new column named “Wholesaler Price” After the price column of the WooCommerce product’s list table.
Helper function to insert an element after a specific array key
If you’re having trouble finding out how to find the name of your custom taxonomy column so you can reorder your columns with custom taxonomies like I was, the index in your $columns array is $columns[taxonomy-{taxonomy_name}].
For instance I had made a ‘Document Category’ custom taxonomy for my ‘Document’ Custom Post Type, so that index for me was $columns[taxonomy-document_category]. So the entire code to order my custom taxonomy with my new ‘Attachment Type’ column was: