Get 50% Discount Offer 26 Days

Recommended Services
Supported Scripts
WordPress
Hubspot
Joomla
Drupal
Wix
Shopify
Magento
Typeo3
How to Add Custom Columns to WordPress Admin Tables and Make Them Sortable

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).
Why Add Custom Columns

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

You can remove default columns by using the manage_{$post_type}_posts_columns filter and unsetting the column you want to remove. For example:

add_filter('manage_posts_columns', 'remove_default_columns');  

function remove_default_columns($columns) {  

    unset($columns['author']);  

    return $columns;  

}

Yes, you can sort by custom meta fields by registering the column as sortable and adjusting the query accordingly, similar to the examples provided.

For multilingual sites, ensure that your custom columns and sorting logic account for translations. To make strings translatable, use functions like __() or _e().

Use the pre_get_users hook to adjust the user query. For example, to sort by a custom meta field:

add_action('pre_get_users', 'sort_users_by_custom_meta');  

function sort_users_by_custom_meta($query) {  

    if ('custom_meta' === $query->get('orderby')) {  

        $query->set('meta_key', 'custom_meta');  

        $query->set('orderby', 'meta_value');  

    }  

}
  • Index Meta Fields: Add database indexes to frequently queried meta keys.
  • Use Transients: Cache column data with set_transient() and get_transient().

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 Author

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.

Leave a Reply

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

Lifetime Solutions:

VPS SSD

Lifetime Hosting

Lifetime Dedicated Servers