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 have custom post type "Book" and custom taxonomy "Author" associated with books. I have used remove_meta_box to hide default UI for taxonomy, because it was like tags where you can add multiple tags and select multiple tags for post.

What i want was that only one author is associated with book. So i used add_meta_box to create select box for authors where admin can select one author from list.

Now authors list has grown beyond 3000 authors and select box does not seems to load it properly.....

I don't want to go back to original UI like tags....

Is there any thing where i could give search, pagination and add functionality(add only one taxonomy) in meta box....

This is code i am using..

remove_meta_box('tagsdiv-book_author', 'book', 'normal');

add_meta_box('custom-taxonomy-author-dropdown','Author','author_dropdowns_box','book','side','high');

function author_dropdowns_box( $post ) {
    wp_nonce_field('custom-author-dropdown', 'author-dropdown-nonce');
    $terms = get_terms( 'book_author', 'hide_empty=0');
    $object_terms = wp_get_object_terms( $post->ID, 'book_author', array('fields'=>'ids'));

     echo "Author:";
    echo "<select id='bauthor' name='bauthor[]'>";
    echo "<option value='0'>None</option>";
    foreach ( $terms as $term ) {

            if ( in_array($term->term_id, $object_terms) ) {
                $parent_id = $term->term_id;
                echo "<option value='{$term->term_id}' selected='selected'>{$term->name}</option>";
            } else {
                echo "<option value='{$term->term_id}'>{$term->name}</option>";
            }

    }
    echo "</select><br />";

}

add_action('save_post','save_my_custom_taxonomy');
share|improve this question
    
can you show us you meta box code? I had a similar problem and i fixed it by "caching the dropdown". –  Bainternet Jul 12 '11 at 11:01

1 Answer 1

Just like i commented , this will create the dropdown if it doesn't exists and will save it in the database

    remove_meta_box('tagsdiv-book_author', 'book', 'normal');

add_meta_box('custom-taxonomy-author-dropdown','Author','author_dropdowns_box','book','side','high');

function author_dropdowns_box( $post ) {
    wp_nonce_field('custom-author-dropdown', 'author-dropdown-nonce');

    echo "Author:";
    echo "<select id='bauthor' name='bauthor[]'>";
    echo "<option value='0'>None</option>";
    $options = get_option('bauthor_dropdown');
    if ($option !== false){
        echo $options;
    }else{
        $options = '';
        $terms = get_terms( 'book_author', 'hide_empty=0');
        $object_terms = wp_get_object_terms( $post->ID, 'book_author', array('fields'=>'ids'));
        foreach ( $terms as $term ) {

                if ( in_array($term->term_id, $object_terms) ) {
                    $parent_id = $term->term_id;
                    $options .= "<option value='".$term->term_id."' selected='selected'>".$term->name."</option>";
                } else {
                    $options .= "<option value='".$term->term_id."'>".$term->name."</option>";
                }

        }
        update_option('bauthor_dropdown',$options);
        echo $options;
    }
    echo "</select><br />";

}

add_action('save_post','save_my_custom_taxonomy');

and all that is left is to clear the list when you add/edit/delete a term of that taxonomy:

//clear the list when a new term is created: or edited
add_action('edit_book_author','clear_on_add',10,2);
add_action('created_book_author','clear_on_add',10,2);
function clear_on_add($term_id, $tt_id){
    delete_option('bauthor_dropdown');
}

//clear the list when a term is deleted:
add_action('delete_term', 'clear_on_delete',10,3);
function clear_on_delete($term, $tt_id, $taxonomy){
    if ($taxonomy == "book_author"){
        delete_option('bauthor_dropdown');
    }
}
share|improve this answer

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.