Created
October 9, 2014 10:30
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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