Skip to content

Instantly share code, notes, and snippets.

@techb
Last active February 15, 2022 23:33
Show Gist options
  • Save techb/4642ad86aeaba501bb928d35555d9497 to your computer and use it in GitHub Desktop.
Save techb/4642ad86aeaba501bb928d35555d9497 to your computer and use it in GitHub Desktop.
wordpress function to add a new column to admin list view of selected post-type and field
<?php
// yoink: https://wordpress.stackexchange.com/a/379641
// add this to functions.php
function add_admin_column($column_title, $post_type, $cb){
// Column Header
add_filter( 'manage_' . $post_type . '_posts_columns', function($columns) use ($column_title) {
$columns[ sanitize_title($column_title) ] = $column_title;
return $columns;
} );
// Column Content
add_action( 'manage_' . $post_type . '_posts_custom_column' , function( $column, $post_id ) use ($column_title, $cb) {
if(sanitize_title($column_title) === $column){
$cb($post_id);
}
}, 10, 2 );
add_filter( 'manage_edit-'. $post_type.'_sortable_columns', function($columns) use ($column_title, $field) {
$columns[$column_title] = $field;
return $columns;
});
}
// example using 'coupon' post type and the 'subtitle' advanced custom field
add_admin_column(__('sub title'), 'coupon', function($post_id){
echo get_post_meta( $post_id , 'coupon_post_subtitle' , true );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment