
When customized, WordPress administration interface becomes easier to manage and can significantly improve user experience. One popular customization is adding custom columns to post or user listing tables, which improves administrator data visibility. This guide will walk you through creating, populating, and sorting custom columns in the WordPress admin, complete with code examples, best practices, and troubleshooting tips.
Why Add Custom Columns?
Custom columns help you:
- Display Hidden Data: Show post metadata (e.g., author, custom fields) or user details (e.g., registration date).
- Improve Workflow: Quickly identify content status (e.g., “Featured Post” flags).
- Enhance Sorting: Sort posts/users by custom criteria (e.g., page views, ratings).
Step 1: Adding Custom Columns to Post Listings
Register the Column
Use the manage_{$post_type}_posts_columns filter to insert a column to the post list. For example, let’s add a “Featured Image” column to the “Posts” screen:
add_filter('manage_posts_columns', 'add_custom_post_columns');Â Â
function add_custom_post_columns($columns) {Â Â
  $columns['featured_image'] = 'Featured Image'; Â
  return $columns; Â
}
Replace posts with your post type slug (e.g., manage_product_posts_columns for a “product” CPT).
Populate the Column
Use the manage_{$post_type}_posts_custom_column action to display content in the column:
add_action('manage_posts_custom _column', 'populate_custom_post_columns', 10, 2);Â Â
function populate_custom_post_columns($column, $post_id) {Â Â
  if ($column === 'featured_image') { Â
    $thumbnail = get_the_post_thumbnail($post_id, 'thumbnail'); Â
    echo $thumbnail ? $thumbnail : 'No Image'; Â
  } Â
}
This code checks if the current column is the “Featured Image” column and retrieves the post thumbnail. If no image exists, it displays “No Image.”
Step 2: Adding Custom Columns to User Listings
Register the Column
To insert a custom column to the user listing, use the manage_users_columns filter:
add_filter('manage_users_columns', 'add_custom_user_columns');Â Â
function add_custom_user_columns($columns) {Â Â
  $columns['registration_date'] = 'Registration Date'; Â
  return $columns; Â
}
Populate the Column
Use the manage_users_custom_column action to fill in the data for the new column:
add_action('manage_users_custom_column', 'populate_custom_user_columns', 10, 3);Â Â
function populate_custom_user_columns($output, $column_name, $user_id) {Â Â
  if ($column_name === 'registration_date') { Â
    $user_info = get_userdata($user_id); Â
    echo date('Y-m-d', strtotime($user_info->user_registered)); Â
  } Â
}
This code retrieves the registration date of the user and formats it for display.
Step 3: Making Columns Sortable for Posts
Register Sortable Columns
To make the custom column sortable, use the manage_edit-{$post_type}_sortable_columns filter:
add_filter('manage_edit-posts_sortable_columns', 'register_sortable_columns');Â Â
function register_sortable_columns($columns) {Â Â
  $columns['featured_image'] = 'featured_image'; Â
  return $columns; Â
}
Adjust the Query for Sorting
Use the pre_get_posts action to modify the query when sorting by the custom column:
add_action('pre_get_posts', 'sort_custom_columns');Â Â
function sort_custom_columns($query) {Â Â
  if (!is_admin() || !$query->is_main_query()) return; Â
  if ('featured_image' === $query->get('orderby')) { Â
    $query->set('meta_key', '_thumbnail_id'); Â
    $query->set('orderby', 'meta_value'); Â
  } Â
}
This code checks if the query is for the admin area and modifies it to sort by the featured image meta key.
Step 4: Making Columns Sortable for Users
Register Sortable Columns
For user columns, use the following code:
add_filter('manage_edit-users_sortable_columns', 'register_sortable_user_columns');Â Â
function register_sortable_user_columns($columns) {Â Â
  $columns['registration_date'] = 'registration_date'; Â
  return $columns; Â
}
Adjust the Query for Sorting
Modify the user query similarly:
add_action('pre_get_users', 'sort_custom_user_columns');Â Â
function sort_custom_user_columns($query) {Â Â
  if (!is_admin() || !$query->is_main_query()) return; Â
  if ('registration_date' === $query->get('orderby')) { Â
    $query->set('orderby', 'user_registered'); Â
  } Â
}
This code allows sorting users by their registration date.
Step 5: Advanced Techniques
Indexing Data
For performance, ensure that any custom fields used for sorting are indexed. This process can significantly speed up queries, especially with large datasets.
Caching Column Data
To reduce database load, consider caching the data displayed in custom columns. You can also temporarily store results using transients or object caching.
AJAX for Dynamic Content
For a more dynamic experience, implement AJAX to load column data without refreshing the page. This implementation can enhance user experience, especially for large datasets.
Step 6: Common Issues and Troubleshooting
Columns Not Appearing
If your custom columns do not show up, check the hook priority and ensure your function is correctly registered.
Sorting Not Working
If sorting does not function as expected, verify that the query is correctly modified and that the data is indexed.
Performance Hits
Avoid heavy queries in custom columns. If you experience low performance, consider optimizing your database or using caching strategies.
FAQs
Conclusion
Adding custom columns to the WordPress admin tables can greatly enhance the management of posts and users. By following the steps outlined in this guide, you can create, populate, and sort custom columns effectively. Remember to consider performance and security best practices while implementing these features. With these enhancements, you can tailor the WordPress admin experience to better suit your needs and improve overall efficiency.
About the writer
Hassan Tahir wrote this article, drawing on his experience to clarify WordPress concepts and enhance developer understanding. Through his work, he aims to help both beginners and professionals refine their skills and tackle WordPress projects with greater confidence.