I have 2 custom post types named clients and casestudies. I’m trying to build a meta box on the clients post type that will have a drop down list featuring the titles of all posts from the casestudies post type. This will end up with a page displaying the featured image from the clients post type, then hyperlink off to the relevant casestudies post if a selection is made from the drop down list.

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

This is the meta box code I have in my 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 which case study this logo will link to when it is 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 the correct post type, but when I update the post it won’t save the data.

Thanks.

Read more here: WordPress metabox drop down list isn’t saving


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: