2

Guys. I wonder if we could customize the all posts page in the admin panel? Please see the pic below.

the all posts page in the admin panel

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.

  • So you'd like additional columns that show your new post meta? – Tom J Nowell Sep 2 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 Sep 3 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 Sep 3 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 Sep 4 at 3:40
0

Please 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.org/Plugin_API/Action_Reference/manage_$post_type_posts_custom_column

https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column

  • 1
    Can you edit your question to explain how it works? Copy pasta might fix an immediate problem, but it doesn't answer the question, and isn't helpful. The asker needs to be able to understand why it fixes the problem – Tom J Nowell Sep 2 at 10:19
  • Hey, Bhupen, nice code. Your answer is off to a good start. Please consider explaining how and why this code solves the problem so others can learn from it. Check the guide to answers for further advice. – Matthew Brown aka Lord Matt Sep 2 at 17:02
  • 2
    Thanks, TomJNowell and @MatthewBrownakaLordMatt I will try to add the answer with proper explanation. – Bhupen Sep 3 at 4:19
  • Thank you, Bhupen. – Gary Hu Sep 4 at 3:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.