Display list of tags as drop down menu or radio buttons in a meta box?

In my last project i had the same issue and i just used this:

first get the list of tags to a var using the get_categories function by passing the right taxonomy like this:

    $args = array(

    'orderby'                  => 'name',
    'order'                    => 'ASC',
    'hide_empty'               => 0,
    'taxonomy'                 => 'post_tag'
    );
$categories=get_categories($args);
    foreach($categories as $category) { 
    $tags[] =  $category->name ;  
    }

then create the arguments for the meta box

$prefix = 'CPT_my_meta';
$meta_box = array(
    'id' => 'custom-meta-box',
    'title' => 'tags',
    'page' => 'CPT name',
    'context' => 'normal',
    'priority' => 'high',
    'fields' => array(
        array(
            'name' => 'tags',
            'desc' => 'select a tag',
            'id' => $prefix . 'name',
            'type' => 'select',
            'options' => $tags
        )))

then add the meta box

add_action('admin_menu', 'add_my_box');

// Add meta box
function add_my_box() {
    global $meta_box;

    add_meta_box($meta_box['id'], $meta_box['title'], 'metabox_callback', $meta_box['page'], $meta_box['context'], $meta_box['priority']);
}

then all you have to do is create a function to show the meta box

//show meta box
function metabox_callback(){
global $meta_box, $post;

    // Use nonce for verification
    echo '<input type="hidden" name="META_BOX_NONEC" value="', wp_create_nonce(basename(__FILE__)), '" />';

    echo '<table class="form-table">';

    foreach ($meta_box['fields'] as $field) {
        // get current post meta data
        $meta = get_post_meta($post->ID, $field['id'], true);

        echo '<tr>',
                '<th style="width:20%"><label for="', $field['id'], '">', $field['name'], '</label></th>',
                '<td>';
        switch ($field['type']) {
            case 'select':
                echo '<select name="', $field['id'], '" id="', $field['id'], '">';
                foreach ($field['options'] as $option) {
                    echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
                }
                echo '</select>';
                break;
        }
        echo    '</td></tr>';
    }
    echo '</table>';
}

and save it on post save

//hook save function
add_action('save_post', 'save_my_meta_box');

// Save data from meta box
function save_my_meta_box($post_id) {
    global $meta_box;

    // verify nonce
    if (!wp_verify_nonce($_POST['META_BOX_NONEC'], basename(__FILE__))) {
        return $post_id;
    }

    // check autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return $post_id;
    }

    // check permissions
    if ('page' == $_POST['post_type']) {
        if (!current_user_can('edit_page', $post_id)) {
            return $post_id;
        }
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;
    }

    foreach ($meta_box['fields'] as $field) {
        $old = get_post_meta($post_id, $field['id'], true);
        $new = $_POST[$field['id']];

        if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], $new);
        } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
        }
    }
}

hope this helps

Leave a Comment