How to customize columns in admin post lists in WordPress

How to customize columns in admin post lists in WordPress
1200 Views
4.1
(66)

Customizing columns in the admin post list in WordPress can help you better manage your content by adding or removing information that is relevant to your specific needs.

By default, WordPress displays columns such as Title, Author, Categories, Tags, Date, and Comments in the post list. However, you may want to display additional information, such as custom fields or taxonomies, or remove columns that are not relevant to your content.

Fortunately, WordPress provides a way to customize the columns in the admin post list through filters and actions that can be added to your theme’s functions.php file or a custom plugin.

By adding code to these filters and actions, you can modify the default columns or add new ones to display the information that is important to you. This can improve your workflow and make it easier to manage your content within the WordPress admin.

Column Filter

<?php
// Add the custom columns to the post in post type:
add_filter( 'manage_post_posts_columns', 'cxc_custom_edit_post_columns' );
function cxc_custom_edit_post_columns($columns) {
	unset( $columns['author'] );
	$columns['image'] = __( 'Image', 'cxc-codexcoach' );
	$columns['post_author'] = __( 'Author', 'cxc-codexcoach' );
	$columns['publisher'] = __( 'Publisher', 'cxc-codexcoach' );
	$columns['verified'] = __( 'Verified', 'cxc-codexcoach' );

	return $columns;
}
?>
<?php
// Add the data to the custom columns for the post in post type:
add_action( 'manage_post_posts_custom_column' , 'cxc_custom_post_custom_column', 10, 2 );
function cxc_custom_post_custom_column( $column, $post_id ) {
	switch ( $column ) {

		case 'image' :
		echo get_the_post_thumbnail( $post_id, array(80, 80) ); 
		break;

		case 'post_author' :
		$terms = get_the_term_list( $post_id , 'post_author' , '' , ',' , '' );
		if ( is_string( $terms ) ){
			echo $terms;
		} else {
			_e( 'Unable to get author(s)', 'cxc-codexcoach' );
		}
		break;

		case 'publisher' :
		echo get_post_meta( $post_id , 'publisher' , true ); 
		break;

		case 'verified' :
		$verified = get_post_meta( $post_id, 'verified', true );
		if ( $verified ) {
			echo '<span style="color:green;">'; _e('Yes', 'cxc-codexcoach'); echo '</span>';
		} else {
			echo '<span style="color:red;">'; _e('No', 'cxc-codexcoach'); echo '</span>';
		}
		break;

	}
}
?>

Removing a column

This code is for how to remove a column from the admin post list in WordPress programmatically using a filter.

The add_filter() the function is used to add a filter to the manage_post_posts_columns hook, which is used to modify the columns that are displayed on the post list in the admin area.

The cxc_custom_post_remove_column() the function is the callback function for the filter. This function takes the $columns parameter, which is an associative array of columns that are currently displayed on the post list, and removes the “Author” column by using the unset() function to remove the corresponding key-value pair from the array.

Finally, the modified $columns the array is returned from the function, which is then used by WordPress to display the updated columns on the post list.

Overall, this code snippet shows how you can use the manage_post_posts_columns filter to customize the columns that are displayed on the post list in the WordPress admin by removing a specific column, in this case, the “Author” column.

<?php
add_filter( 'manage_post_posts_columns', 'cxc_custom_post_remove_column' ) ;
function cxc_custom_post_remove_column( $columns ) {
	unset( $columns['author'] );
	return $columns;
}
?>

Reordering columns

The add_filter() a function is used to add a filter to the manage_post_posts_columns hook, which is used to modify the columns that are displayed on the post list in the admin area.

Finally, the modified $columns the array is returned from the function, which is then used by WordPress to display the updated columns on the post list with the “Author” column moved to the end.

Overall, this code snippet shows how you can use the manage_post_posts_columns filter to customize the columns that are displayed on the post list in the WordPress admin by changing the order of a specific column, in this case, the “Author” column.

<?php
add_filter( 'manage_post_posts_columns', 'cxc_custom_post_change_reordering_column' );
function cxc_custom_post_change_reordering_column( $columns ) {
	$reordering_column = $columns['author'];
	unset( $columns['author'] );
	$columns['author'] = $reordering_column;
	return $columns;
}
?>

Custom column as sortable

<?php
add_filter( 'manage_edit-post_sortable_columns', 'cxc_manage_post_sortable_columns'  );
function cxc_manage_post_sortable_columns( $columns ) {
	$columns['author'] = 'author';
	return $columns;
}

add_action( 'pre_get_posts', 'cxc_pre_get_posts_author' );
function cxc_pre_get_posts_author( $query ) {
	if (!is_admin()) {
		return;
	}

	$orderby = $query->get('orderby');
	if ( $orderby == 'author' ) {
		$query->set('meta_key', 'author');
		$query->set('orderby', 'meta_value_num');
	}
}
?>

Disable sorting for default columns

<?php
add_filter( 'manage_edit-post_sortable_columns', 'cxc_manage_remove_post_sortable_columns' );
function cxc_manage_remove_post_sortable_columns( $columns ) {	
	unset( $columns['author'] );
	return $columns;
}
?>

Conclusion

Fortunately, WordPress provides a way to customize the columns in the admin post list through filters and actions that can be added to your theme’s functions.php file or a custom plugin. By adding code to these filters and actions, you can modify the default columns or add new ones to display the information that is important to you.

ALSO READ:

How to add custom fields in admin edit user and profile in WordPress

How to Hide, Remove, and Disable Add to cart button in WooCommerce

How to Disable the Gutenberg Editor in WordPress

FAQs

Can I remove default columns from the post list page in WordPress?

Yes, you can remove default columns from the post list page in WordPress by using the unset() function on the column you want to remove in the manage_posts_columns hook.

How can I add custom columns to the post list page in WordPress?

To add custom columns to the post list page in WordPress, use the manage_posts_columns hook to add the column header, and the manage_posts_custom_column hook to display the content of the column for each post.

How can I display custom metadata in a column on the post list page in WordPress?

To display custom metadata in a column on the post list page in WordPress, you can use the get_post_meta() function in the manage_posts_custom_column hook to retrieve the data for each post and display it in the column.

Can I add an image to a column on the post list page in WordPress?

Yes, you can add an image to a column on the post list page in WordPress by using the manage_posts_custom_column hook to output a <img> tag with the source set to the URL of the image you want to display.

How useful was this blog?

Click on a star to rate it!

Average rating 4.1 / 5. Vote count: 66

No votes so far! Be the first to rate this blog.

1 comment

  1. I have fun with, lead to I discovered exactly what I was looking for. You have ended my four day lengthy hunt! God Bless you man. Have a great day. Bye

Leave a comment

Your email address will not be published. Required fields are marked *