3

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

   }
1
  • 1
    Not unclear, but lacks research efforts. Searching here and consulting the Codex can give you the solution. At least, a starting point.
    – brasofilo
    Jul 12, 2013 at 13:31

1 Answer 1

6

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.

2
  • Thank you for that info! Should get me on the right track. After I get it setup, I'll post the result here!
    – mtuttle
    Jul 12, 2013 at 15:50
  • Ok, I came up with this so far. Think I might still be off a bit since the "Select Post Type" drop down displays the wrong choice after saving. Here's my code:
    – mtuttle
    Jul 12, 2013 at 19:30

Your Answer

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

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