Skip to content

Instantly share code, notes, and snippets.

@eccentricpixel
Created October 9, 2014 10:30
Show Gist options
  • Save eccentricpixel/5c35af7a3912357215e5 to your computer and use it in GitHub Desktop.
Save eccentricpixel/5c35af7a3912357215e5 to your computer and use it in GitHub Desktop.
add custom field as a column in post list and make it sortable.
//Add custom column
add_filter('manage_edit-YOURPOSTTYPENAME_columns', 'my_columns_head');
function my_columns_head($defaults) {
$defaults['YOURCOLUMNNAME'] = 'YOURCOLUMNTITLE';
return $defaults;
}
//Add rows data
add_action( 'manage_YOURPOSTTYPENAME_posts_custom_column' , 'my_custom_column', 10, 2 );
function my_custom_column($column, $post_id ){
switch ( $column ) {
case 'YOURCOLUMNNAME':
echo get_post_meta( $post_id , 'wpcf-YOURCUSTOMFIELDNAME' , true );
break;
}
}
// Make these columns sortable
function sortable_columns() {
return array(
'YOURCOLUMNNAME' => 'YOURCOLUMNNAME'
);
}
add_filter( "manage_edit-YOURPOSTTYPENAME_sortable_columns", "sortable_columns" );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment