0

I created a custom field bookcode using the code below. So, how I can add a column for this meta data into my post type manager and make it sortable?

function save_bookcode( $post_id ) {
    if ( wp_is_post_revision( $post_id ) )
        return;
    // save custom field 'bookcode' as CFX+ID
    add_post_meta( $post_id, 'bookcode', 'CFX' . $post_id, true );
}
add_action( 'save_post', 'save_bookcode' );
2
  • That is the name of the post type (or post types) that you'd like to this column to? Jul 8, 2017 at 20:07
  • 1
    @DaveRomsey my CPT is 'stfic'
    – Democloak
    Jul 8, 2017 at 20:25

1 Answer 1

1

I adjusted the code for you. You can directly use the code in functions.php file. Here is more reference that can help you to understand more. Link 1 Link 2

// Adding Custom Column
add_filter('manage_stfic_posts_columns', 'ST4_columns_head_only_stfic', 10);
add_action('manage_stfic_posts_custom_column', 'ST4_columns_content_only_stfic', 10, 2);

// CREATE TWO FUNCTIONS TO HANDLE THE COLUMN
function ST4_columns_head_only_stfic($defaults) {
    $defaults['bookcode'] = 'Book Code';
    return $defaults;
}
function ST4_columns_content_only_stfic($column_name, $post_ID) {
    if ($column_name == 'bookcode') {

        // Now you have to garb the meta value and show.            
        $book_code = get_post_meta($post_ID,'bookcode',true);
        echo '<span>'.$book_code.'</span>';
    }
}
2
  • Ah yes!!! It worked perfect! Now I'll try to learn from this and add rating field into CPT admin. Thank you!!!
    – Democloak
    Jul 8, 2017 at 20:36
  • Good to hear that it resolved your issue. Jul 8, 2017 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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