Ok, I have a meta box setup now that populates with the all the registered post types. That part works fine. But, upon selecting an option in the dropdown list, and saving / updating, the value shows the first choice when the page reloads. Also, does anyone know if get_post_types can exclude specific post types?

Here's my code:

    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', 'page', 'normal', 'high' );

    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] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );

    <label for="my_meta_box_select">Post type: </label>
    <select name="my_meta_box_select" id="my_meta_box_select">
        <?php $post_types=get_post_types('', 'objects'); foreach ($post_types as $post_type): ?>
        <option value="<?php echo esc_attr($post_type->name);?>"  <?php selected( $selected, '$post_type->name' ); ?>><?php echo esc_html($post_type->label); selected ?></option>
        <?php endforeach; ?>


    add_action( 'save_post', 'cd_meta_box_save' );
    function cd_meta_box_save( $post_id )
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post', $post_id ) ) return;

// now we can actually save the data
$allowed = array( 
    'a' => array( // on allow a tags
        'href' => array() // and those anchords can only have href attribute

// Probably a good idea to make sure your data is set

if( isset( $_POST['my_meta_box_select'] ) )
    update_post_meta( $post_id, 'my_meta_box_select', esc_attr( $_POST['my_meta_box_select'] ) );


1 Answer 1


Variables do not expand inside single quotes, so this-- selected( $selected, '$post_type->name'-- is literally checking for a string that looks like '$post_type->name'. If you are saving the option names, it should work if you just remove the quotes (though I did not run your code to test that).

Your might be able to exclude what you want via the public and _builtin arguments to get_post_types, but if you are looking to exclude a particular type other than what those arguments will do, I don't see a way to do that directly, but there is a filter. It would likely be easier just to filter the post type out in your foreach loop though.

  • Awesome! That was exactly what I needed! I'd vote your answer up, but I don't have the rep.
    – mtuttle
    Jul 15, 2013 at 17:32
  • OK, I have one more question related to the code above. I need to call the value from the dropdown and place it in a query. How would you go about adding the value from the drop down to the query code below: <?php $wp_query->query("post_type=nemultispecies&paged=".$paged.'&showposts=5'); ?>
    – mtuttle
    Jul 15, 2013 at 18:58
  • Use get_post_meta( $post_id, 'my_meta_box_select', true);, if I understand what you are asking.
    – s_ha_dum
    Jul 15, 2013 at 19:03
  • Ok, I tried this code but I don't think it's working out for me: <?php $wp_query->query("post_type=<?php get_post_meta( $post_id, 'my_meta_box_select', true); ?>&paged=".$paged.'&showposts=5'); ?>
    – mtuttle
    Jul 15, 2013 at 19:30
  • Got it! <?php $cpt = get_post_meta($post->ID, "my_meta_box_select", true); ?> <?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query(); ?> <?php $wp_query->query("post_type= '.$cpt.'&paged=".$paged.'&showposts=5'); ?>
    – mtuttle
    Jul 15, 2013 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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