1

I'm currently working on a website that has a custom post type ("schedules") that requires custom hierarchal taxonomies (named "location" and "servicetype" in this case) to be displayed as radio buttons instead of checklists, as demonstrated in this tutorial.

Here, I had to modify the code to prevent certains terms of the taxonomy that are only being used by other custom post types and not this one, from showing. This is why I didn't want to use a plugin for that.

However, what happens is that whenever I try to update or save a post from this post type, using the radio button-based taxonomy system, the chosen taxonomy terms doesn't get saved into the post.

PHP:

add_action( 'admin_menu', 'braintrain_remove_metaboxes');
function braintrain_remove_metaboxes(){
   remove_meta_box('locationdiv','schedules', 'normal');
   remove_meta_box('servicetypediv', 'schedules', 'normal');
}

add_action( 'add_meta_boxes', 'braintrain_add_metaboxes');
function braintrain_add_metaboxes() {
    add_meta_box( 'location_selector', 'Locations','braintrain_locations_metabox', 'schedules' ,'side','core');
    add_meta_box( 'servicetype_selector', 'Services', 'braintrain_servicetype_metabox', 'schedules' ,'side','core');
}

function braintrain_locations_metabox( $post ) {
$taxonomy = 'location';

$tax = get_taxonomy($taxonomy);
$terms = get_terms($taxonomy,array('hide_empty' => 0));

$name = 'tax_input[' . $taxonomy . ']';

$popular = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
$postterms = get_the_terms( $post->ID,$taxonomy );
$current = ($postterms ? array_pop($postterms) : false);
$current = ($current ? $current->term_id : 0);

?>

<div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv" >

    <div id="<?php echo $taxonomy; ?>-all" class="tabs-panel" style="border:0; overflow:visible; background-color: transparent; max-height: 100%; padding: 0; margin:0;">
        <ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy; ?> categorychecklist form-no-clear" style="margin:0;">
            <?php   foreach($terms as $term) { $termChildren = get_term_children($term->term_id, $taxonomy);
                $id = $taxonomy.'-'.$term->term_id;

                if ( count($termChildren) === 0 ) {
                    echo "<li id='$id'><label class='selectit'>";
                    echo "<input type='radio' id='in-$id' name='{$name}'".checked($current,$term->term_id,false)."value='$term->term_id' />$term->name<br />";
                    echo "</label></li>";
                }

            } ?>
       </ul>
    </div>

</div>
<?php
}

function braintrain_servicetype_metabox( $post ) {
    $taxonomy = 'servicetype';

    $tax = get_taxonomy($taxonomy);
    $terms = get_terms($taxonomy,array('hide_empty' => 0, 'parent' => 0));

    $name = 'tax_input[' . $taxonomy . ']';

$popular = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
$postterms = get_the_terms( $post->ID,$taxonomy );
$current = ($postterms ? array_pop($postterms) : false);
$current = ($current ? $current->term_id : 0);

?>

<div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">

    <div id="<?php echo $taxonomy; ?>-all" class="tabs-panel" style="border:0; overflow:visible; background-color: transparent; max-height: 100%; padding: 0; margin:0;">
        <ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy; ?> categorychecklist form-no-clear" style="margin:0;">
            <?php   foreach($terms as $term){
                $id = $taxonomy.'-'.$term->term_id; $termChildren = get_term_children($term->term_id, $taxonomy);

                if ( count($termChildren) >= 0 ) {
                echo "<li id='$id'><label class='selectit'>";
                echo "<input type='radio' id='in-$id' name='{$name}'".checked($current,$term->term_id,false)."value='$term->term_id' />$term->name<br />";
                echo "</label></li>";
                }
            }?>
       </ul>
    </div>

</div>

<?php
}

1 Answer 1

0

If your taxonomy is non-hierarchical like tags, change value of input to $term->name. Took me a while to figure it out. It's crazy but wordpress handle non-hierarchical taxonomies by their name in forms and not ids.

echo "<input type='radio' id='in-$id' name='{$name}'".checked($current,$term->term_id,false)."value='$term->name' />$term->name<br />";

Your Answer

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

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