manage_{$post_type}_posts_columns
云策文档标注
概述
manage_{$post_type}_posts_columns 是一个 WordPress 过滤器钩子,用于自定义特定文章类型在文章列表表格中显示的列。它允许开发者添加、移除或重新排列列,以优化后台管理界面。
关键要点
- 钩子名称是动态的,其中 $post_type 替换为文章类型的 slug,例如 manage_post_posts_columns 或 manage_page_posts_columns。
- 参数 $posts_columns 是一个关联数组,键为列名(用于回调函数识别),值为列标签(显示为表头)。
- 内置列类型包括 cb(复选框)、title(标题)、author(作者)、categories(分类)、tags(标签)、comments(评论数)和 date(日期),默认按顺序显示。
- 通过此钩子可以修改列数组,例如使用 unset() 移除列、array_merge() 添加新列或重新排序数组来调整列顺序。
- 自定义列时需注意避免与内置列名冲突,建议使用前缀以防止默认行为覆盖。
代码示例
// 示例:为自定义文章类型“book”添加列并移除作者列
function add_book_columns($columns) {
unset($columns['author']);
return array_merge($columns,
array('publisher' => __('Publisher'),
'book_author' =>__( 'Book Author')));
}
add_filter('manage_book_posts_columns' , 'add_book_columns');注意事项
- 自定义列名应避免与内置列(如“tags”)重复,否则可能导致回调函数不被触发,建议添加前缀以确保唯一性。
- 对于自定义分类法列,列名格式为 taxonomy-{taxonomy_name},例如 $columns['taxonomy-document_category']。
- 列的顺序可以通过重新构建数组来控制,例如使用 $custom_col_order 数组来指定列的顺序。
原文内容
Filters the columns displayed in the Posts list table for a specific post type.
Description
The dynamic portion of the hook name, $post_type, refers to the post type slug.
Possible hook names include:
manage_post_posts_columnsmanage_page_posts_columns
Parameters
$posts_columnsstring[]-
An associative array of column headings.
Source
return apply_filters( "manage_{$post_type}_posts_columns", $posts_columns );
Changelog
| Version | Description |
|---|---|
| 3.0.0 | Introduced. |
Skip to note 8 content
Andrija Naglic
function my_custom_columns_list($columns) { unset( $columns['title'] ); unset( $columns['author'] ); unset( $columns['date'] ); $columns['product_number'] = 'Product Number'; $columns['custom_handler'] = 'Nice name'; return $columns; } add_filter( 'manage_product_posts_columns', 'my_custom_columns_list' );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
Skip to note 9 content
Steven Lin
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.
function add_book_columns($columns) { unset($columns['author']); return array_merge($columns, array('publisher' => __('Publisher'), 'book_author' =>__( 'Book Author'))); } add_filter('manage_book_posts_columns' , 'add_book_columns');Skip to note 10 content
Shashikant Yadav
To reorder columns
function wpdocs_item_columns( $columns ) { $custom_col_order = array( 'cb' => $columns['cb'], 'title' => $columns['title'], 'rating' => __( 'Ratings', 'textdomain' ), 'date' => $columns['date'] ); return $custom_col_order; } add_filter( 'manage_item_posts_columns', 'wpdocs_item_columns' );Just use the
$custom_col_orderarray to reorder the columns in{$post_type}.NOTE: in the above example,
itemis the custom post typeSkip to note 11 content
gregrobson
Be careful with your choice of column name.
I decided to use
tagsas 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.
Skip to note 12 content
Steven Lin
Example migrated from Codex:
Replace Columns
Here’s another example that completely replaces the columns, rather than adding and removing specific ones.
function set_book_columns($columns) { return array( 'cb' => '<input type="checkbox" />', 'title' => __('Title'), 'comments' => '<span class="vers comment-grey-bubble" title="' . esc_attr__( 'Comments' ) . '"><span class="screen-reader-text">' . __( 'Comments' ) . '</span></span>', 'date' => __('Date'), 'publisher' => __('Publisher'), 'book_author' =>__( 'Book Author') ); } add_filter('manage_book_posts_columns' , 'set_book_columns');Skip to note 13 content
Alberuni Azad.
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.
add_filter( 'manage_product_posts_columns', 'wpdocs_wholesale_price_column', 50 ); function wpdocs_wholesale_price_column( $columns ) { // Insert wholesale price column after the price column $columns = wpdocs_insert_element_after_specific_array_key( $columns, 'price', array( 'key' => 'wholesale_price', 'value' => __( 'Wholesale Price', 'text-domain' ), ) ); return $columns; }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; }Skip to note 14 content
nickylind
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:
public function wpdocs_document_columns($columns) { $custom_col_order = array( 'cb' => $columns['cb'], 'title' => $columns['title'], 'taxonomy-document_category' => $columns['taxonomy-document_category'], 'attachment_type' => $columns['attachment_type'], 'date' => $columns['date'] ); return $custom_col_order; } add_filter( 'manage_document_posts_columns', 'wpdocs_document_columns' );