php - Wordpress metabox drop down list isn't saving -


i have 2 custom post types named clients , casestudies. i'm trying build meta box on clients post type have drop down list featuring titles of posts casestudies post type. end page displaying featured image clients post type, hyperlink off relevant casestudies post if selection made drop down list.

i have followed tutorial meta box put together: http://code.tutsplus.com/tutorials/how-to-create-custom-wordpress-writemeta-boxes--wp-20336

this meta box code have in functions.php file:

add_action( 'add_meta_boxes', 'cd_meta_box_add' ); function cd_meta_box_add() {     add_meta_box( 'my-meta-box-id', 'my first meta box', 'cd_meta_box_cb', 'clients', 'side', 'default' ); }  function cd_meta_box_cb( $post ) { $values = get_post_custom( $post->id ); $selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;     ?>          <p>         <label for="my_meta_box_select">select case study logo link when clicked:<br /><br /></label>         <select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">             <option value="no case study">no case study</option>                 <?php                 $casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );                 $casestudiesloop = new wp_query( $casestudies );                 while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();                     ?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>             <?php                 endwhile;                     ?>         </select>     </p>     <?php     }  add_action( 'save_post', 'cd_meta_box_save' ); function cd_meta_box_save( $post_id ) {     if( defined( 'doing_autosave' ) && doing_autosave ) return;      if( !isset( $_post['meta_box_nonce'] ) || !wp_verify_nonce( $_post['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;      if( !current_user_can( 'edit_post', $post_id ) ) return;      if( isset( $_post['my_meta_box_select'] ) )         update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_post['my_meta_box_select'] ) ); } 

the meta box displays correctly on correct post type, when update post won't save data.

thanks.

you don't have nonce hidden field. save function return nothing.

<input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" /> 

update:

so cd_meta_box_cb function be

<?php     function cd_meta_box_cb( $post )     {         $values = get_post_custom( $post->id );         $selected = isset( $values['my_meta_box_select'] ) ? esc_attr( $values['my_meta_box_select'][0] ) : ”;         ?>         <p>             <label for="my_meta_box_select">select case study logo link when clicked:<br /><br /></label>             <select name="my_meta_box_select" id="my_meta_box_select" style="width:100%;">                 <option value="no case study">no case study</option>                     <?php                     $casestudies = array( 'post_type' => 'casestudies', 'orderby' => 'title', 'order' => 'asc', );                     $casestudiesloop = new wp_query( $casestudies );                     while ( $casestudiesloop->have_posts() ) : $casestudiesloop->the_post();                         ?> <option value="<?php the_title(); ?>" <?php selected( $selected, $casestudies['the_title'] ); ?> ><?php the_title(); ?></option>                 <?php                     endwhile;                         ?>             </select>         </p>         <input type="hidden" name="meta_box_nonce" id="meta_box_nonce" value="<?php echo wp_create_nonce( 'my_meta_box_nonce' ); ?>" />         <?php     } ?> 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -