Creating a Custom Post Types dropdown in a Meta Box

Is it possible to populate a meta box with a drop down list of custom post types? I plan on trying to create a template for a client that will allow them to create a new page in the dashboard, and then select a custom post type from the CPT meta box to choose what posts are displayed on said page.

Update: Here’s as far as I’ve gotten right now. The post types box is not returning the value after saving. Any help would be appreciated.

    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' );
?>

<p>
    <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); selected ?>"><?php echo esc_html($post_type->label); selected ?></option>
        <?php endforeach; ?>
    </select>
</p>    

<p>  
    <label for="my_meta_box_select">Color</label>  
    <select name="my_meta_box_select" id="my_meta_box_select">  
        <option value="red" <?php selected( $selected, 'red' ); ?>>Red</option>  
        <option value="blue" <?php selected( $selected, 'blue' ); ?>>Blue</option>  
    </select>  
</p>     

<?php   
   }


   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' ) ) 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'] ) );

   }

Related posts

1 comment

  1. Of course it is possible.

    Below is code that will display registered post types:

    function my_meta_box_add() {
        add_meta_box( 'my-meta-box-id', 'MY meta', 'my_meta_box', 'page', 'normal', 'high' );
    } add_action( 'add_meta_boxes', 'my_meta_box_add' );
    
    
    function my_meta_box( $post ) {
        ?>
        <p>
            <label for="my_meta_box_post_type">Post type: </label>
            <select name='my_meta_box_post_type' id='my_meta_box_post_type'>
                <?php $post_types=get_post_types('', 'objects'); foreach ($post_types as $post_type): ?>
                <option value="<?php echo esc_attr($post_type->name); ?>"><?php echo esc_html($post_type->name); ?></option>
                <?php endforeach; ?>
            </select>
        </p>
        <?php   
    }
    

    Of course you have to take care of saving this value, checking nonces, and so on.

Comments are closed.