Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I would like to group posts of a custom post type based on tags, but the default functionality does not do for the project I am working on.

I want the user to be able to select only one tag, not multiple tags, from a list of all the tags entered for that custom post type (drop down or radio buttons).

The user can create as many tags as he wants from the page for adding a custom taxonomy, and all these tags will be listed in the meta box on the single custom post page.

Any suggestion?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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

share|improve this answer
    
This is awesome! Thank you very much! I will give it a try immediately ;) –  Mirko Jan 21 '11 at 10:02
    
There are metabox actions available specifically so you can hook on and add metaboxes, so i'm wondering why you're using admin_menu, some appropriate actions would be add_meta_boxes, add_meta_boxes-%POST_TYPE%, and do_meta_boxes.. –  t31os Jan 21 '11 at 14:51
    
you are 100% right! –  Bainternet Jan 21 '11 at 16:05
    
I guess the problem is in the registration of my custom taxonomy...it is not registered yet when get_categories is called.... –  Mirko Jan 21 '11 at 16:11
    
I am pretty sure that this code does not save the selected values as a taxonomy, but instead it does so as a custom field. So it uses the list of terms from a given taxonomy, but then saves the selected term as a custom field and retrieve that custom field. –  didibus Feb 5 '12 at 3:21
show 1 more comment

I tried to show the list and it does work if you use post_tag or category, but it gives me the list of the default post type tags or categories.

I want to have the list of the custom taxonomies for my custom posts. My custom post type is "au-gallery" and my custom taxonomy is "gallery" therefore I tried

$args = array( 'type' => 'au-gallery', 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 0, 'taxonomy' => 'gallery' );

but I got only an empty list.

I had a look at get_the_term_list and get_the_terms but those functions requires the post ID so...do I need to run a loop for al my custom posts to get this list or is there a better way??

share|improve this answer
    
Try get_terms( 'taxonomyNAMEhere', $args ).. where $args is an array of the appropriate arguments, see: codex.wordpress.org/Function_Reference/get_terms –  t31os Jan 21 '11 at 14:53
    
I am trying right now, thank you ;) –  Mirko Jan 21 '11 at 15:22
    
I tried, but again it does not work with custom taxonomies –  Mirko Jan 21 '11 at 15:32
    
Actually, taxonomy_exists('gallery') gives false, but everything is fine in my admin panel...I can see my custom taxonomies and add/edit them –  Mirko Jan 21 '11 at 15:55
    
remove 'type' => 'au-gallery', from your args array, and you\ll be fine –  Bainternet Jan 21 '11 at 16:09
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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