WordPress: Sortable Custom Columns
This entry is part of my WordPress development series, providing tutorials on specific things you can do in WordPress. Read some more?
Continuing on from the previous tutorial where we added custom columns, we are now going to make these columns sortable like the regular default columns in WordPress (such as post/page title and publish date).
For simplicity, we will continue to work in our functions.php file. You can edit this file by going to Appearance > Editor in your WordPress admin panel, or using a text editor with FTP.
From the last tutorial, I have already created my custom columns for my custom post type “Cats”. So first we have to tell WordPress to make our columns sortable using the function add_filter()
.
// Make columns sortable add_filter( 'manage_edit-cf_cats_sortable_columns', 'cats_sortable_cols' ); function cats_sortable_cols( $columns ) { $columns['age'] = 'age'; $columns['toy'] = 'toy'; return $columns; }