Skip to content

How to Add a Sortable Column to a Custom Post Type in WordPress

By ali 6 min read

Add a sortable Event Date column to a WordPress custom post type admin list

WordPress allows you to add custom columns to the admin list table for posts, pages, and custom post types. You can also make these columns sortable so that administrators can organize records based on custom field values.

For example, imagine you have a custom post type called event and each event has a custom field named event-date. You can display this date in the Events admin list and allow users to sort events by date.

In this tutorial, we will create a custom Event Date column and make it sortable without installing an additional plugin.

Steps to Create a Sortable Column

  • Register a custom column for the post type.
  • Display the custom field value in that column.
  • Register the column as sortable.
  • Modify the admin query to sort records using the custom field.

We will use the WordPress admin column hooks and the pre_get_posts action to implement this functionality.

1. Register a Custom Column for the Post Type

First, we need to add a new column to the Events admin list table.

For a custom post type with the slug event, use the manage_edit-event_columns filter.

<?php

/**
 * Register a custom Event Date column.
 */
function geekerhub_event_date_column_register( $columns ) {

    $columns['event-date'] = __( 'Event Date', 'geekerhub' );

    return $columns;
}

add_filter(
    'manage_edit-event_columns',
    'geekerhub_event_date_column_register'
);

This adds a column named Event Date to the Events admin list table.

2. Display the Custom Column Content

After registering the column, we need to display its value.

We will retrieve the event date from the post meta field named event-date using get_post_meta().

<?php

/**
 * Display the Event Date column value.
 */
function geekerhub_event_date_column_display( $column_name, $post_id ) {

    if ( 'event-date' !== $column_name ) {
        return;
    }

    $event_date = get_post_meta(
        $post_id,
        'event-date',
        true
    );

    if ( empty( $event_date ) ) {
        echo esc_html__( 'Not set', 'geekerhub' );
        return;
    }

    echo esc_html( $event_date );
}

add_action(
    'manage_event_posts_custom_column',
    'geekerhub_event_date_column_display',
    10,
    2
);

The manage_event_posts_custom_column action runs when WordPress displays custom columns for the Event post type.

The callback checks the column name before retrieving and displaying the corresponding custom field value. We also use esc_html() to safely output the value.

3. Register the Column as Sortable

Now that the custom column is visible, we need to make it sortable.

WordPress provides the manage_edit-event_sortable_columns filter for registering sortable columns in the Event admin list.

<?php

/**
 * Register Event Date as a sortable column.
 */
function geekerhub_event_date_column_register_sortable( $columns ) {

    $columns['event-date'] = 'event-date';

    return $columns;
}

add_filter(
    'manage_edit-event_sortable_columns',
    'geekerhub_event_date_column_register_sortable'
);

The array key identifies the custom column, while its value tells WordPress which orderby parameter to use when the column heading is clicked.

In this example, WordPress will use event-date as the value of the orderby query parameter.

4. Modify the Admin Query to Sort by the Custom Field

Registering a column as sortable only tells WordPress which query parameter to use. We still need to configure the admin query to sort the Events using the corresponding post meta value.

We can do this using the pre_get_posts action. It allows us to modify the query before WordPress retrieves the posts.

<?php

/**
 * Sort Events by the Event Date custom field.
 */
function geekerhub_event_date_column_orderby( $query ) {

    if ( ! is_admin() || ! $query->is_main_query() ) {
        return;
    }

    if ( 'event' !== $query->get( 'post_type' ) ) {
        return;
    }

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

        $query->set( 'meta_key', 'event-date' );
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'meta_type', 'DATE' );
    }
}

add_action(
    'pre_get_posts',
    'geekerhub_event_date_column_orderby'
);

This code checks that the query is the main admin query for the Event post type. It then checks whether the selected sorting option is event-date.

If the conditions match, WordPress uses the event-date post meta field to sort the records.

The meta_type is set to DATE, so the values are compared as dates rather than ordinary text. For this example, store dates in the YYYY-MM-DD format, such as 2026-11-03.

5. Complete Code for the Sortable Custom Post Type Column

You can combine all four steps into one snippet and add it to your child theme’s functions.php file or a custom plugin.

<?php

/**
 * Register the Event Date column.
 */
function geekerhub_event_date_column_register( $columns ) {

    $columns['event-date'] = __( 'Event Date', 'geekerhub' );

    return $columns;
}

add_filter(
    'manage_edit-event_columns',
    'geekerhub_event_date_column_register'
);


/**
 * Display the Event Date column value.
 */
function geekerhub_event_date_column_display( $column_name, $post_id ) {

    if ( 'event-date' !== $column_name ) {
        return;
    }

    $event_date = get_post_meta(
        $post_id,
        'event-date',
        true
    );

    if ( empty( $event_date ) ) {
        echo esc_html__( 'Not set', 'geekerhub' );
        return;
    }

    echo esc_html( $event_date );
}

add_action(
    'manage_event_posts_custom_column',
    'geekerhub_event_date_column_display',
    10,
    2
);


/**
 * Register the Event Date column as sortable.
 */
function geekerhub_event_date_column_register_sortable( $columns ) {

    $columns['event-date'] = 'event-date';

    return $columns;
}

add_filter(
    'manage_edit-event_sortable_columns',
    'geekerhub_event_date_column_register_sortable'
);


/**
 * Modify the admin query to sort by Event Date.
 */
function geekerhub_event_date_column_orderby( $query ) {

    if ( ! is_admin() || ! $query->is_main_query() ) {
        return;
    }

    if ( 'event' !== $query->get( 'post_type' ) ) {
        return;
    }

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

        $query->set( 'meta_key', 'event-date' );
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'meta_type', 'DATE' );
    }
}

add_action(
    'pre_get_posts',
    'geekerhub_event_date_column_orderby'
);

After adding the code, open the Events section in your WordPress dashboard. You should see the Event Date column. Click its heading to sort the events by date. Click it again to switch between ascending and descending order.

Important Things to Consider

  • Custom post type slug: Replace event with your actual custom post type slug in the hooks and query condition.
  • Meta key: Use the correct custom field name. In this example, the meta key is event-date.
  • Date format: Store dates in a sortable format such as YYYY-MM-DD when using the DATE meta type.
  • Missing values: Posts without an Event Date may not appear in the same way as posts with a date when sorting by post meta. Test this behavior if some events do not have a date.
  • Existing code: If another plugin or custom function modifies the same admin query, check for conflicts.

If you are building custom post type functionality inside your own plugin, see our guide on creating a custom plugin in WordPress.

You can also refer to the official WordPress documentation for admin post type columns and pre_get_posts to learn more about modifying admin queries.

Conclusion

WordPress makes it possible to add sortable custom columns to the admin list table using its built-in hooks and filters.

By registering the column, displaying its custom field value, marking it as sortable, and modifying the admin query, you can allow administrators to sort custom post type records by fields such as event dates, prices, or other metadata.

You can use the same approach for other custom post types by changing the post type slug, column name, and meta key to match your project.