How to add sortable custom columns in admin post lists in WordPress
Learn how to boost your WordPress admin experience by adding a custom column to the post list and making it sortable. Follow our step-by-step guide for seamless customization.
Learn how to boost your WordPress admin experience by adding a custom column to the post list and making it sortable. Follow our step-by-step guide for seamless customization.
WordPress provides a user-friendly admin interface, but customizing it to your specific needs can save time and streamline your content management process. Adding a custom column allows you to display additional information relevant to your content, making it easier to manage and edit posts.
Adding a custom column
To begin, we’ll use the manage_{$post_type}_posts_columns
filter to insert our custom column into the post list. Open your theme’s functions.php
file and add the following code:
add_filter( 'manage_post_posts_columns', 'post_additional_columns' );
function post_additional_columns($columns) {
// Add your custom column
$columns['custom_column'] = 'Custom Column';
return $columns;
}
The column will be added at the end of the list. If you want to add it somewhere in the middle add this code:
/**
* Additional columns.
*
* @param $columns
*
* @return array
*/
function post_additional_columns( $columns ) {
return array(
'cb' => $columns['cb'],
'title' => $columns['title'],
'author' => $columns['author'],
'categories' => $columns['categories'],
'tags' => $columns['tags'],
'custom_column' => __( 'Custom column', 'projectsengine' ),
'date' => $columns['date']
);
}
This way, the custom column will be added just before the date column.
Populating the custom column
Now, let’s populate the custom column with relevant data. Use the manage_{$post_type}_posts_custom_column
action hook to display content within the custom column. In this example, we’ll retrieve and display custom data stored as post meta:
add_action( 'manage_post_posts_custom_column', 'post_additional_columns_values', 10, 2 );
/**
* Display the additional values.
*
* @param $column_key
* @param $post_id
*
* @return void
*/
function post_additional_columns_values( $column_key, $post_id ) {
if( 'custom_column' === $column_key ) {
echo get_post_meta( $post_id, '_custom_column', true );
}
}
Remember to replace ‘_custom_column’ with the actual meta key storing your custom data.
Making the custom column sortable
This sets up the ability to sort your posts based on the values in the custom column. In a way enables the column to be sortable and clickable.
function custom_column_sortable($columns) {
// Make your custom column sortable
$columns['custom_column'] = 'custom_column';
return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'custom_column_sortable', 10, 1 );
Sorting your custom column adds a valuable layer of functionality. To achieve this, modify the pre_get_posts
filter in your functions.php
file:
add_filter( 'pre_get_posts', 'modify_pre_get_posts', 10, 1 );
/**
*
* @param $query
*
* @return mixed
*/
function modify_pre_get_posts( $query ) {
if( is_admin() ):
$post_type = $query->query['post_type'];
if( 'post' === $post_type && isset( $_GET['orderby'] ) && isset( $_GET['order'] ) && 'custom_column' === $_GET['orderby'] ):
$order = $_GET['order'];
$query->set( 'meta_key', 'custom_column' );
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'order', $order);
endif;
endif;
return $query;
}
By following these steps, you’ve successfully added a custom column to the post list in the WordPress admin and made it sortable.
Was this post helpful? ( Answers: 1 )
Leave a comment
If you enjoyed this post or have any questions, please leave a comment below. Your feedback is valuable!