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 used the answer in this question to show a dropdown list of my taxonomies and it works great.

But when I save the post/update and the page refreshes/loads the taxonomy I selected reverts back to the last taxonomy on the list. I checked the database and it does save the chosen taxonomy correctly so it's obviously how I echo the data right? How can I fix this so that it stays on the selected taxonomy?

$term = isset( $values['meta-box-events_select'] ) ? esc_attr( $values['meta-box-events_select'][0] ) : '';

THIS IS THE CODE I USED TO SHOW THE DROPDOWN

        <?php $tax = get_object_taxonomies('activities');
        $taxterms = get_terms( $tax, 'orderby=count&offset=1&hide_empty=0&fields=all' );
    ?>
    <select name='meta-box-events_select' id='meta-box-events_select'>
        <option value='' <?php if (!count( $names )) echo "selected";?>>Select Term</option>
        <?php 
        foreach ( $taxterms as $term ) { 
            echo '<option value="' . $term->slug . '" selected>' . $term->name . '</option>',"\n"; 
        } ?>
    </select>
share|improve this question
add comment

1 Answer 1

It seems you are adding the term 'selected' to each option - you only want it added to the currently chosen option.

You can use the WordPress built-in function, selected, which allows you to compare two values (say the saved term's ID and the current options term ID) and when they match prints 'selected'.

For instance:

<?php
$current_term;//Retrieve the current saved term's;
//Get the current term's ID, or 0 if it doesn't exist
$current_term_id = ($current_term ? $current_term->term_id : 0); 
?>

<select name='meta-box-events_select' id='meta-box-events_select'>
 <option value='' <?php selected(0,$current_term_id)?>>Select Term</option>
        <?php 
        foreach ( $taxterms as $term ) { 
            echo '<option value="'.$term->slug.'"'. selected($term->term_id,$current_term->term_id).'>'. $term->name . '</option>'; 
        } ?>
 </select>
share|improve this answer
    
Thanks. I eventually got it working. –  kolorguild Aug 7 '12 at 10:33
add comment

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.