Back to Snippets

Add Custom Admin Columns for a CPT

PHP Admin & Dashboard April 6, 2026

Add custom sortable admin columns to any post type — render post meta in the admin list table with proper escaping.

Snippet Stats

Lines 37
Characters 1,148
Read 2 mins
php • 37 lines
/**
 * Add custom columns to the portfolio admin list.
 */
add_filter( 'manage_portfolio_posts_columns', function( array $columns ): array {
    $new = array();
    foreach ( $columns as $key => $label ) {
        $new[ $key ] = $label;
        if ( 'title' === $key ) {
            $new['project_url']  = __( 'Project URL', 'theme-domain' );
            $new['project_year'] = __( 'Year', 'theme-domain' );
        }
    }
    return $new;
} );

/**
 * Render the custom column content.
 */
add_action( 'manage_portfolio_posts_custom_column', function( string $column, int $post_id ): void {
    switch ( $column ) {
        case 'project_url':
            $url = get_post_meta( $post_id, '_project_url', true );
            echo $url ? '' . esc_html( $url ) . '' : '—';
            break;
        case 'project_year':
            echo esc_html( get_post_meta( $post_id, '_project_year', true ) ?: '—' );
            break;
    }
}, 10, 2 );

/**
 * Make the Year column sortable.
 */
add_filter( 'manage_edit-portfolio_sortable_columns', function( array $columns ): array {
    $columns['project_year'] = 'project_year';
    return $columns;
} );

Found an issue with this snippet? Help us improve by reporting it. Report it →

Related Snippets

View all
JavaScript
This reusable Higher Order Component handles asynchronous data fetching in React applications, making it ideal for fetching data from APIs on componen...

React Higher Order Component For Asynchronous Data Fetching And Error Handling

JavaScript
This React code snippet demonstrates a centralized global state management pattern using the Context API, simplifying state access and updates across ...

React Context API Pattern For Managing Global State In Large-Scale Applications

PHP
This code snippet provides a custom WordPress hook to update user profiles. It is useful when you need to synchronize user data with external services...

WordPress Custom Hook For User Profile Updates