Guys. I wonder if we could customize the all posts page in the admin panel? Please see the pic below.
I created some custom fields before and am thinking if the fields could be shown in the blank space, just like category and also be used when we sort the posts.
Thank you.
Guys. I wonder if we could customize the all posts page in the admin panel? Please see the pic below.
I created some custom fields before and am thinking if the fields could be shown in the blank space, just like category and also be used when we sort the posts.
Thank you.
Share Improve this question asked Sep 2, 2019 at 9:16 Gary HuGary Hu 253 bronze badges 4- So you'd like additional columns that show your new post meta? – Tom J Nowell ♦ Commented Sep 2, 2019 at 10:17
- Yes, Tom. Also, I would like to sort the posts according to the values of post meta, just as we can sort them by authors and categories. – Gary Hu Commented Sep 3, 2019 at 3:01
- sorting by post meta was never mentioned in your question, you should ask a brand new question for how to sort by post meta – Tom J Nowell ♦ Commented Sep 3, 2019 at 8:46
- Sorry, Tom but I'm going to study the code and links Bhupen has provided for me. Hope I can figure it out on my own. Thank you. – Gary Hu Commented Sep 4, 2019 at 3:40
1 Answer
Reset to default 0Please try the code given below to add custom columns to default post type to display custom field data and make changes to the code as per your requirement.
// Add the custom columns
add_filter( 'manage_posts_columns', 'set_custom_edit_posts_columns' );
function set_custom_edit_posts_columns($columns) {
$columns['first_field_name'] = 'Custom Field First';
$columns['second_field_name'] = 'Custom Field Second';
return $columns;
}
// Add the data to the custom columns
add_action( 'manage_posts_custom_column' , 'custom_posts_column', 10, 2 );
function custom_posts_column( $column, $post_id ) {
switch ( $column ) {
case 'first_field_name' :
echo 'First Field Data';
break;
case 'second_field_name' :
echo 'Second Field Data';
break;
}
}
Here "manage_posts_columns" is used to add the columns. You can change post type by adding custom post type name in "manage_your_post_type_name_posts_columns". And "manage_posts_custom_column" will populate columns data. Where you can display your custom fields data. And here also you can set the post type by adding custom post type name in "manage_your_post_type_name_posts_custom_column".
https://codex.wordpress/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column
https://codex.wordpress/Plugin_API/Action_Reference/manage_posts_custom_column