I've made a custom post type and registered it. I need some more fields for this post type, and would like to add 2 more with the specific keys that will load anytime a new post is made (IE like another excerpt field, but it will say Title/position). I'm not sure how to do this, and would like to avoid plugins. Any help would be greatly appreciated.
$labels = array(
'name' => _x('Executive', 'post type general name '),
'singular' => _x('Executive', 'post type singular name'),
'add_new' => _x('Add new', 'text_domain'),
'add_new_item' => __( 'Add Executive', 'text_domain' ),
'name_admin_bar' => __( 'Staff', 'text_domain' ),
'archives' => __( 'Staff Archives', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Exectives', 'text_domain' ),
'new_item' => __( 'New Item', 'text_domain' ),
'edit_item' => __( 'Edit Item', 'text_domain' ),
'update_item' => __( 'Update Item', 'text_domain' ),
'view_item' => __( 'View Item', 'text_domain' ),
'search_items' => __( 'Search Item', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into item', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Items list', 'text_domain' ),
'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
'menu_name' => __( 'Executives', 'text_domain' ),
);
$args = array(
'label' => __( 'Executive', 'text_domain' ),
'description' => __( 'Executive Team', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'author','name','excerpt','thumbnail', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => false,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => false,
'capability_type' => 'page',
);
register_post_type('Executives', $args);
}
add_action('init', 'custom_post_executive');
You need to register a custom meta box with a callback that will render the HTML. You can read more about it here: https://developer.wordpress.org/plugins/metadata/custom-meta-boxes/
I would also recommend looking at the plugin Advanced Custom Fields (ACF) as they make creating meta fields trivial.
function wporg_add_custom_box()
{
$screens = ['post', 'wporg_cpt'];
foreach ($screens as $screen) {
add_meta_box(
'wporg_box_id', // Unique ID
'Custom Meta Box Title', // Box title
'wporg_custom_box_html', // Content callback, must be of type callable
$screen // Post type
);
}
}
add_action('add_meta_boxes', 'wporg_add_custom_box');
function wporg_custom_box_html($post)
{
?>
<label for="wporg_field">Description for this field</label>
<select name="wporg_field" id="wporg_field" class="postbox">
<option value="">Select something...</option>
<option value="something">Something</option>
<option value="else">Else</option>
</select>
<?php
}