Add Custom Column to Custom Post Types

Advertisement

We can add extra columns to the custom post types with:

  • Filter manage_<post-type>_posts_columns
  • Action manage_<post-type>_posts_custom_column

Add a custom column One & Two to the post.

Register Custom Columns Register Custom Columns

if( ! function_exists( 'prefix_add_column' ) ) :
	function prefix_add_column( $columns ) {
	    $columns['prefix-column-one'] = __( 'One', 'text-domain' );
	    $columns['prefix-column-two'] = __( 'Two', 'text-domain' );
	    return $columns;
	}
	add_filter( 'manage_post_posts_columns', 'prefix_add_column' );
endif;

Top ↑

Display Values in Columns Display Values in Columns

if( ! function_exists( 'prefix_display_column_value' ) ) :
	function prefix_display_column_value( $column ) {
	    switch ( $column ) {
	        case 'prefix-column-one' :
	            echo 'Prefix Column One';
	            break;
	        case 'prefix-column-two' :
	            echo 'Prefix Column Two';
	            break;
        }
	}
	add_action( 'manage_post_posts_custom_column', 'prefix_display_column_value' );
endif;

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.