Skip to content

Instantly share code, notes, and snippets.

@eccentricpixel
Created December 10, 2014 08:32
Show Gist options
  • Save eccentricpixel/50133b7842b027d9829e to your computer and use it in GitHub Desktop.
Save eccentricpixel/50133b7842b027d9829e to your computer and use it in GitHub Desktop.
Add a column to a custom post type manage screen and make it sortable (replace "book" for your post type and "release_date" for your desired field name). Use switch statement in the ep_get_book_column_values function if you add additional columns in the ep_add_book_columns function.
/* Manipulate the BOOKS ADMIN columns
* ================================================== */
add_filter('manage_edit-book_columns', 'ep_remove_unwanted_columns');
add_filter('manage_edit-book_columns', 'ep_add_book_columns', 5);
add_action('manage_book_posts_custom_column', 'ep_get_book_column_values', 5, 2);
// Remove unwanted columns
function ep_remove_unwanted_columns($defaults){
unset($defaults['author']);
return $defaults;
}
// Add new columns
function ep_add_book_columns($defaults){
// field vs displayed title
$defaults['release_date'] = __('Release Date');
return $defaults;
}
// Populate the new columns with values
function ep_get_book_column_values($column_name, $postID){
if($column_name === 'release_date'){
echo get_post_meta($postID,'release_date',TRUE);
}
}
// make it sortable
add_filter( 'manage_edit-book_sortable_columns', 'my_book_sortable_columns' );
function my_book_sortable_columns( $columns ) {
$columns['release_date'] = 'release_date';
return $columns;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment