The fourth argument for add_meta_box()
is the context, which in this case would be teams
, not latest discussion
or next fixture
as you have here. Also, since you want these metaboxes to show on the new and edit screens for your CPT only, it’s easiest to register a metabox callback for your specific CPT and get rid of the action on admin_init
. Add it to your $args array, e.g. 'register_meta_box_cb' => 'prefix_teams_cpt_add_metaboxes'
. So, a modified version of your code:
// Register Teams
add_action('init', 'register_teams');
function register_teams() {
$labels = array(
'name' => _x('Teams', 'post type general name'),
'singular_name' => _x('Team', 'post type singular name'),
'add_new' => _x('Add New', 'team item'),
'add_new_item' => __('Add New Team'),
'edit_item' => __('Edit Team'),
'new_item' => __('New Team'),
'view_item' => __('View Team'),
'search_items' => __('Search Teams'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'menu_icon' => get_stylesheet_directory_uri() . '/images/menu-teams.png',
'supports' => array( 'title', 'editor', 'thumbnail' ),
'has_archive' => true,
'register_meta_box_cb' => 'prefix_teams_cpt_add_metaboxes'
);
register_post_type( 'teams' , $args );
}
// Add Metaboxes
function prefix_teams_cpt_add_metaboxes(){
add_meta_box("latest_discussion", "Latest Discussion", "prefix_latest_discussion_metabox", "teams", "side", "low");
add_meta_box("next_fixture", "Next Fixture", "prefix_next_fixture_metabox", "teams", "side", "low");
}
function prefix_latest_discussion_metabox(){
global $post;
$custom = get_post_custom($post->ID);
$latest_discussion = $custom["latest_discussion"][0];
?>
<label>Latest Discussion URL:</label><br /><br />
<input size="40" name="latest_discussion" value="<?php echo $latest_discussion; ?>" />
<?php
}
function prefix_next_fixture_metabox(){
global $post;
$custom = get_post_custom($post->ID);
$next_fixture = $custom["next_fixture"][0];
?>
<label>Next Fixture:</label><br /><br />
<input size="40" name="next_fixture" value="<?php echo $next_fixture; ?>" />
<?php
}
// Save Fields
add_action('save_post', 'prefix_save_teams_details');
function prefix_save_teams_details(){
global $post;
update_post_meta($post->ID, "latest_discussion", $_POST["latest_discussion"]);
update_post_meta($post->ID, "next_fixture", $_POST["next_fixture"]);
}
Note that I’ve changed some function names – be sure that you always prefix your functions, and it’s way easier to keep track of things like a metabox creating function if the name really describes what it does.