• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (deprecated)
You are here: Home / Draft / Add WordPress Posts Column

Add WordPress Posts Column

Last updated on July 2, 2020 by Sal Ferrarello

Code to add a custom column to the WP Admin Posts listing page.

WP Admin posts listing with additional column for Thumbnail ID.

Example Code to add a Custom WordPress Column

namespace salcode\addPostMetaColumn;

add_filter( 'manage_post_posts_columns', __NAMESPACE__ . '\add_column', 20 );
add_action( 'manage_posts_custom_column', __NAMESPACE__ . '\render_column', 10, 2 );

/**
 * Add thumbnail ID to posts display.
 *
 * @param array $columns Columns.
 * @return array Updated columns.
 */
function add_column( $columns ) {
    return array_merge(
        array_slice( $columns, 0, 2, true ),
        [
            'thumbnail_id' => 'Thumbnail ID',
        ],
        array_slice( $columns, 2, null, true )
    );
}

/**
 * Render the content for the thumbnail ID.
 *
 * @param string $column  Column slug.
 * @param int    $post_id Post ID.
 */
function render_column( $column, $post_id ) {
    if ( 'thumbnail_id' === $column ) {
        echo esc_html( get_post_meta( $post_id, '_thumbnail_id', true ) );
    }
}

Easy Mistake to Make

The biggest thing I forget is that I need to use the manage_post_posts_columns hook (rather than manage_posts_columns). Using the manage_posts_columns hook applies my custom column to all post types (e.g. post and custom post types).

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.
Warning! This is a draft, not a finalized post. See full draft disclosure.

Filed Under: Draft, Programming, Solution Tagged With: WordPress, WordPress Filter

Reader Interactions

Leave a Reply Cancel reply

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

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in