WordPress Snippets at WPcustoms

Show custom post type meta info in admin panel

If you created your own custom post types this may be a nice snippet to display its data in your WordPress admin panel. In this example we got an event post type.
The first function only adds the table. The second fills it with content. $column_name == ‘ticket_status’ is equal to our table header name and the content comes from the meta box field: get_post_meta( $post_id, ‘meta_event_ticket_status’, true ).
Take care with the two action hooks we are using: manage_event_posts_columns and manage_event_posts_custom_column – replace EVENT with the your custom post type slug. i.e. manage_movies_posts_columns


/**
 * Snippet Name: Show custom post type meta info in admin panel
 * Snippet URL: https://wpcustoms.net/snippets/show-custom-post-type-meta-info-in-admin-panel/
 */
  // adding our table columns with this function:
function wpc_custom_table_head( $defaults ) {
    $defaults['event_date']  = 'Event Date';
    $defaults['ticket_status']    = 'Ticket Status';
    $defaults['venue']   = 'Venue';
    $defaults['author'] = 'Added By';
    return $defaults;
}
// change the _event_ part in the filter name to your CPT slug
add_filter('manage_event_posts_columns', 'wpc_custom_table_head');




// now let's fill our new columns with post meta content
function wpc_custom_table_content( $column_name, $post_id ) {
    if ($column_name == 'event_date') { // refering name to our table header $defaults
		$event_date = get_post_meta( $post_id, 'meta_event_date', true );
			echo  date( _x( 'F d, Y', 'Event date format', 'textdomain' ), strtotime( $event_date ) );
    }
    if ($column_name == 'ticket_status') {
		$status = get_post_meta( $post_id, 'meta_event_ticket_status', true ); // grab the name of your meta box field name
		echo $status;
    }

    if ($column_name == 'venue') {
		echo get_post_meta( $post_id, 'meta_event_venue', true );
    }

}
// change the _event_ part in the filter name to your CPT slug
add_action( 'manage_event_posts_custom_column', 'wpc_custom_table_content', 10, 2 );