Add custom columns to WordPress admin posts list

Add custom columns to the admin posts list

The list of posts in the admin shows title, author, categories, tags, comments and date. Often the information editors check most is somewhere else: a price in a custom field, a reading time, the status of a review, a featured image. A column for it saves opening every post. Two hooks add the column and its content, a third makes it sortable.

Problem: Editors want to see the reading time stored in a custom field directly in the list of posts, and sort by it.

Solution: Add the column header with the manage_{post_type}_posts_columns filter, print the cell with the manage_{post_type}_posts_custom_column action, register it as sortable with manage_edit-{post_type}_sortable_columns, and handle the sorting in pre_get_posts.

The column

<?php
add_filter( 'manage_post_posts_columns', function ( $columns ) {
    // insert after the title instead of at the end
    $new = array();
    foreach ( $columns as $key => $label ) {
        $new[ $key ] = $label;
        if ( 'title' === $key ) {
            $new['ha_reading_time'] = __( 'Reading time', 'textdomain' );
        }
    }
    return $new;
} );

add_action( 'manage_post_posts_custom_column', function ( $column, $post_id ) {
    if ( 'ha_reading_time' === $column ) {
        $minutes = (int) get_post_meta( $post_id, 'ha_reading_time', true );
        echo $minutes ? esc_html( sprintf( __( '%d min', 'textdomain' ), $minutes ) ) : '&mdash;';
    }
}, 10, 2 );

For another post type, replace post in the hook names with its slug, for example manage_product_posts_columns. Pages use manage_page_posts_columns.

The cells are printed for every row, and WordPress has already loaded the meta of all posts on the page, so get_post_meta() here costs no extra queries.

Sorting

<?php
add_filter( 'manage_edit-post_sortable_columns', function ( $columns ) {
    $columns['ha_reading_time'] = 'ha_reading_time'; // the value becomes ?orderby=ha_reading_time
    return $columns;
} );

add_action( 'pre_get_posts', function ( $query ) {
    if ( ! is_admin() || ! $query->is_main_query() || 'ha_reading_time' !== $query->get( 'orderby' ) ) {
        return;
    }

    $query->set( 'meta_key', 'ha_reading_time' );
    $query->set( 'orderby', 'meta_value_num' );
} );

With meta_key set, posts without the field disappear from the sorted list. If that is a problem, sort with a meta_query that has an EXISTS and a NOT EXISTS clause combined with OR, and order by the named clause.

Removing columns

The same filter can remove columns nobody uses:

<?php
add_filter( 'manage_post_posts_columns', function ( $columns ) {
    unset( $columns['comments'], $columns['tags'] );
    return $columns;
}, 20 );

Users can also hide columns themselves in Screen Options at the top of the list; a removed column cannot be brought back there.

NOTE: Escape everything you print in a column. The list is an admin screen, but the values come from post meta that authors, imports or other plugins can write, and an unescaped value in the list is a stored XSS that runs in the browser of every editor who opens it.