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.

Custom Field values not are not showing in respective fields while i click the "edit" button .. they just show blank as we are creating a new post.I am able to save the values in custom field while editing too ... but still the saved values are not being shown in the fields(here the "id" text and the "membership" radio button) while "editing" the post, the id field shows blank and the radio button shows the default selected one

Can somebody point out what i did wrong here?
Here is the code:

add_action( 'init', 'codex_custom_init' );
add_action( 'admin_menu','member_manager_add_meta' );
add_action( 'save_post', 'member_manager_save_extras' );
add_filter( "manage_edit-member_columns", "change_columns" );
add_action( "manage_member_posts_custom_column", "custom_columns", 10, 2 );

function codex_custom_init() {
    $labels = array(
        'name' => _x('Members', 'post type general name'),
        'singular_name' => _x('Member', 'post type singular name'),
        'add_new' => _x('Add New', 'Member'),
        'add_new_item' => __('Add New Member'),
        'edit_item' => __('Edit Member'),
        'new_item' => __('New Member'),
        'all_items' => __('All Members'),
        'view_item' => __('View Member'),
        'search_items' => __('Search Members'),
        'not_found' =>  __('No members found'),
        'not_found_in_trash' => __('No members found in Trash'), 
        'parent_item_colon' => '',
        'menu_name' => __('Members')
    );
    $args = array(
        'labels' => $labels,
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true, 
        'show_in_menu' => true, 
        'query_var' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'has_archive' => true, 
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array( 'title')
    ); 
    register_post_type('Member',$args);
}


function member_manager_add_meta(){
    add_meta_box("members-meta", "Member Details","member_manager_meta_options",'member','normal','high');
}


function member_manager_meta_options(){
    global $post;

    $values =get_post_custom($post->ID);
    $id = isset( $values["isprp_id" ]) ? $values["isprp_id" ] : ' ';
    $membership = isset( $values["isprp_member" ]) ? esc_attr(  $values["isprp_member" ]): '';

    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
        <div class="member_manager_extras">
        <style type="text/css">
        <?php include('member_manager.css'); ?>
        </style>
            <div><label for="isprp_id">ISPRP ID</label><input type="text" name="isprp_id" id="isprp_id"  value ="<?php echo $id; ?>" /></div>

        <div><label for="isprp_member">Membership Category</label>
        <input type="radio" name="isprp_member"  value="Life Member" <?php selected($membership,'Life Member'); ?>checked />Life Member<input type="radio" name="isprp_member"  value="Associate Member" <?php selected($membership,'Associate Member'); ?> />Associate  Member</div>
    <?php
 }

 // Save the Data


function member_manager_save_extras($post_id){
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return; 

    else if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) 
        return; 

    else if( !current_user_can( 'edit_post' ) ) 
        return $post_id; 

    else{
        update_post_meta($post_id, "isprpid",$_POST["isprp_id"]);
        update_post_meta($post_id, "isprpmember",$_POST["isprp_member"]);
    }
}


function change_columns($cols){
    $cols =array(       
        'cb' => '<input type="checkbox" />',
        'title'=>('Name'),
        'isprpid' => __('ID','trans'),
        'isprpmember' =>__('Membership','trans')        
    );

    return $cols;
}

function custom_columns($column,$post_id){
    switch ($column){
    case 'isprpid':
        $isprpid = get_post_meta( $post_id, 'isprpid', true);
       echo '<a href="' . $isprpid . '">' . $isprpid. '</a>';
       break;
    case 'isprpmember':
        $isprpmember = get_post_meta( $post_id, 'isprpmember', true);
       echo '<a href="' . $isprpmember . '">' . $isprpmember. '</a>';
       break;
    }
}
share|improve this question

1 Answer 1

Whenever in doubt about the functioning of WordPress functions, consult the Codex:
http://codex.wordpress.org/Function_Reference/register_post_type

There, we can see that when registering a post type the supports argument has many options, one of them custom-fields.

So, this would enable the CF box for the CPT:

$args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => true,
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'custom-fields' )
); 

Also, there's a missing closing </div> in the function member_manager_meta_options.

[UPDATE]

I'll mark this Q&A for closure as too localized. The problems with it are errors in the post meta key names and wrong reading of a variable values.

If the following debug info is dumped inside the function member_manager_meta_options, you'll see why no values are being read (because they are being saved).

$values =get_post_custom($post->ID);
echo '<pre>' . print_r( $values,true ) . '</pre>';

So, a working function would be:

function member_manager_meta_options(){
    global $post;  
    $values =get_post_custom($post->ID);
    //echo '<pre>' . print_r( $values,true ) . '</pre>';
    $id = isset( $values["isprp_id" ]) ? esc_attr($values["isprp_id" ][0]) : ' ';
    $member_life = ( 'Life Member' == $values["isprp_member" ][0] ) ? 'checked' : '';
    $member_assoc = ( 'Associate Member' == $values["isprp_member" ][0] ) ? 'checked' : '';
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
        <div class="member_manager_extras">
        <style type="text/css">
        <?php include('member_manager.css'); ?>
        </style>
            <div>
                 <label for="isprp_id">ISPRP ID</label>
                 <input type="text" name="isprp_id" id="isprp_id"  value ="<?php echo $id; ?>" />
            </div>
            <div>
                 <label for="isprp_member">Membership Category</label>
                 <input type="radio" name="isprp_member"  value="Life Member" <?php echo $member_life; ?> />Life Member
                 <input type="radio" name="isprp_member"  value="Associate Member" <?php echo $member_assoc; ?> />Associate  Member
            </div>
    <?php
}

And the problem of the post_meta key names is that isprp_member and isprpmember (and ispr_id as well) are not consolidated through the code. They are being used as Columns IDs and as Post Meta Keys.

share|improve this answer
2  
+1 for referencing the codex. Most problems can be solved by simply reading the documentation. –  Chris_O Jul 28 '12 at 8:31
    
Hi Brasofilo , thank you for your concern on my question .. i think i put it in a vague way .. i am getting my metabox and custom fields on my screen and able to save the values too, but when i click the "edit"link the values are not shown in the respective custom fields( they just show blank as in "add new"). I had tried putting custom-fields in supports args before but it is showing the custom field metabox along with my metabox .. –  Anoop D Jul 31 '12 at 11:03
    
@AnoopD So, please edit your Question, adding there the clarification you are making here in the Comments. If you have doubts about it, check this FAQ section. –  brasofilo Jul 31 '12 at 11:10
    
@brasofilo edited the post .. thank you –  Anoop D Jul 31 '12 at 13:12
    
@AnoopD - Answer updated with explanation of what's going wrong. When the pointed corrections are made, the full code works ok. –  brasofilo Jul 31 '12 at 14:25

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.