Last updated on September 14, 2023

Make Custom Admin Column Sortable by Value

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Make custom admin columns sortable.

To make a custom admin column sortable by its value in WordPress, you need to define custom sorting logic and specify how to handle the sorting when the user clicks on the column header. Here’s how you can make a custom admin column sortable:

  1. Register your custom columns: The first step is to register your custom column. See: Add Admin Column to Custom Post Type or Custom Columns Admin Area
  2. Define the Custom Column Data: After registering your column, in your code where you’re adding the custom column, make sure that you have the data for that column. In your case, let’s assume you’re adding a custom column called “Custom Column” and you want to sort by a meta value stored under the custom_data meta key.
  3. Register Custom Sortable Columns: You need to register the custom column as sortable by adding it to the list of sortable columns using the manage_{$screen_id}_sortable_columns filter hook. The dynamic {$screen_id}part of the hook, should be replaced with the screen ID of the admin screen where the columns are shown. You can get the screen data (including the ID) from the WP_Screen object returned by get_current_screen(), for example:
$screen_id = get_current_screent()->id;

For posts, you can use the hook manage_edit-{$post_type}_sortable_columns. Here’s an example:

function wpsnippets_register_sortable_custom_column( $sortable_columns ) {
    $sortable_columns['custom_column'] = 'custom_data'; // Replace 'custom_column' with your actual column ID and 'custom_data' with the meta key you want to sort by.
    return $sortable_columns;
}

// The filter hook for any post type is 'manage_edit-{$post_type}_sortable_columns'
// Replace 'shop_order' in the example below with the slug of the post type the columns are displayed for
add_filter( 'manage_edit-shop_order_sortable_columns', 'wpsnippets_register_sortable_custom_column' );
  1. Handle Sorting Query: You need to handle the sorting query to make sure WordPress knows how to order the posts based on your custom column. This is done using the pre_get_posts action. Here’s an example:
function wpsnippets_handle_custom_column_sorting( $query ) {
    if ( ! is_admin() || ! $query->is_main_query() ) {
        return;
    }

    // Replace 'custom_data' with the meta key you are sorting by
    $meta_key = 'custom_data';

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

add_action( 'pre_get_posts', 'wpsnippets_handle_custom_column_sorting' );

Now, your custom admin column should be sortable by its value when users click on the column header. Make sure to adjust the column ID (custom_column) and the meta key to sort by (custom_data) according to your specific implementation.

Last updated on September 14, 2023. Originally posted on September 11, 2023.

Leave a Reply

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