manage_(post_type)_posts_columns
Allows you to change the list of registered columns in the Posts table.
The hook name is generated dynamically for each post type. For example:
manage_post_posts_columnsfor thepostpost type.manage_page_posts_columnsfor thepagepost type.manage_product_posts_columnsfor theproductpost type (WooCommerce products).- And so on.
See also these similar hooks:
- manage_pages_columns — fires for the
pagepost type. - manage_posts_columns — fires for other post types.
Usage
add_filter( 'manage_(post_type)_posts_columns', 'wp_kama_manage_post_type_posts_columns_filter' );
/**
* Function for `manage_(post_type)_posts_columns` filter-hook.
*
* @param string[] $posts_columns An associative array of column headings.
*
* @return string[]
*/
function wp_kama_manage_post_type_posts_columns_filter( $posts_columns ){
// filter...
return $posts_columns;
}
- $post_columns(array)
Array of column names. For Posts, the default array looks like this:
Array ( [cb] => <input type="checkbox" /> [title] => Title [author] => Author [categories] => Categories [tags] => Tags [comments] => <span class="vers comment-grey-bubble" title="Comments"><span class="screen-reader-text">Comments</span></span> [date] => Date )
Examples
#1 Remove the Author column from Posts
The Author column can be hidden in the Screen Options section, which adds the hidden CSS class to its table cells. The code below unregisters the column entirely, so its data is not output at all.
add_filter( 'manage_post_posts_columns', function ( $columns ) {
unset( $columns['author'] );
return $columns;
} );
#2 Add ID and Thumbnail columns to Posts
// Register the ID and Thumbnail columns. Required.
add_filter( 'manage_post_posts_columns', function ( $columns ) {
$my_columns = [
'id' => 'ID',
'thumb' => 'Thumbnail',
];
return array_slice( $columns, 0, 1 ) + $my_columns + $columns;
} );
// Output content for each registered custom column. Required.
add_action( 'manage_post_posts_custom_column', function ( $column_name ) {
if ( $column_name === 'id' ) {
the_ID();
}
if ( $column_name === 'thumb' && has_post_thumbnail() ) {
?>
<a href="<?php echo get_edit_post_link(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
</a>
<?php
}
} );
// Add styles for the registered columns. Optional.
add_action( 'admin_print_footer_scripts-edit.php', function () {
?>
<style>
.column-id {
width: 50px;
}
.column-thumb img {
max-width: 100%;
height: auto;
}
</style>
<?php
} );Changelog
| Since 3.0.0 | Introduced. |
Where the hook is called
manage_(post_type)_posts_columns
wp-admin/includes/class-wp-posts-list-table.php 755
return apply_filters( "manage_{$post_type}_posts_columns", $posts_columns );
