Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type for 'Teams' set up using the code below. However, the fields for 'Latest Discussion' and 'Next Fixture' isn't showing up in the dashboard. I went over the code numerous times and can't find anything wrong with it. I was hoping someone here could help me figure it out.

// 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_post_type( 'teams' , $args );
}

// Add Fields
add_action("admin_init", "add_teams_fields");

function add_teams_fields(){
    add_meta_box("latest_discussion", "Latest Discussion", "latest_discussion", "latest discussions", "side", "low");
    add_meta_box("next_fixture", "Next Fixture", "next_fixture", "next fixture", "side", "low");
}

function latest_discussion(){
    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 next_fixture(){
    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', 'save_teams_details');

function 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"]);
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

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.

share|improve this answer
    
Thank you very much, Helen. And also, thanks for the prefix tip. I'll keep that in mind for best practices from now on. :) –  SamY Jul 9 '11 at 0:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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