You can use WordPress hooks such as manage_posts_columns or manage_${post_type}_posts_columns to customize the standard columns. For example, to add a new column for the post type “Books”, the code looks like this:
function my_custom_book_columns($columns) {
// Neue Spalte hinzufügen
$columns['book_author'] = __('Autor');
return $columns;
}
add_filter('manage_book_posts_columns', 'my_custom_book_columns');This code adds a new column called “Author” to the admin panel of the custom post type “Books”.
After you have added the column, you must fill the contents of this column for each row in the list of posts. You can do this with the manage_posts_custom_column hook:
function my_custom_book_column_content($column, $post_id) {
if ($column == 'book_author') {
$author = get_post_meta($post_id, 'book_author', true);
echo esc_html($author);
}
}
add_action('manage_book_posts_custom_column', 'my_custom_book_column_content', 10, 2);Here, the value of the user-defined field book_author, which is saved for each post, is displayed in the new “Author” column.
It is also possible to make columns sortable, which is particularly useful if you want to sort by a specific author or date, for example. To do this, add the filter manage_edit-${post_type}_sortable_columns:
function my_sortable_book_columns($columns) {
$columns['book_author'] = 'book_author';
return $columns;
}
add_filter('manage_edit-book_sortable_columns', 'my_sortable_book_columns');This code makes the “Author” column sortable. Please note, however, that an additional pre_get_posts hook is required for sorting to work.
function my_book_column_orderby($query) {
if (!is_admin()) {
return;
}
$orderby = $query->get('orderby');
if ('book_author' == $orderby) {
$query->set('meta_key', 'book_author');
$query->set('orderby', 'meta_value');
}
}
add_action('pre_get_posts', 'my_book_column_orderby');This additional code ensures that the user-defined field book_author is sorted.
If you want to change the width of the columns, you can do this with CSS. You can extend the WordPress admin area with the admin_head hook:
function my_custom_column_width() {
echo '<style>
.column-book_author { width: 20%; }
</style>';
}
add_action('admin_head', 'my_custom_column_width');The width of the “Author” column is set to 20 % here.
A common use case is adding custom columns for a custom plugin or theme. Here’s another example of how you could add the rating and date of the review for a custom post type “Reviews”:
function my_custom_review_columns($columns) {
$columns['review_rating'] = __('Bewertung');
$columns['review_date'] = __('Rezensionsdatum');
return $columns;
}
add_filter('manage_review_posts_columns', 'my_custom_review_columns');
function my_custom_review_column_content($column, $post_id) {
switch ($column) {
case 'review_rating':
$rating = get_post_meta($post_id, 'review_rating', true);
echo esc_html($rating);
break;
case 'review_date':
$date = get_post_meta($post_id, 'review_date', true);
echo esc_html($date);
break;
}
}
add_action('manage_review_posts_custom_column', 'my_custom_review_column_content', 10, 2);In this example, you add two new columns — “Rating” and “Review date” — for the post type “Reviews” and fill them with custom fields.