WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I followed this tutorial to create a custom meta box with a simple two option drop down.

My code is below. The custom fields are saved when creating a post, and edit when I change the value and Update. But when I go to the edit post page the values aren't dynamic and displaying the correct drop down value based on the set meta value. So if I create a post and set the option to Yes, then I go edit to post it shows No but in the custom meta field it does show Yes so it's saving correctly. I know that the No and Yes values should be dynamic and check if any custom meta has been set to display it but not sure what the code is. Any help with the code is appreciated. Thanks

<?  

add_action('admin_menu', 'my_post_options_box');

function my_post_options_box() {
    add_meta_box('post_info', 'Trade', 'custom_post_info', 'post', 'side', 'high');
}

//Adds the actual option box
function custom_post_info() {
    global $post;
?>
    <fieldset id="mycustom-div">
        <div>
        <p>
            <label for="cpi_dropdown_options" >Accept Trade?</label><br />
            <select name="cpi_dropdown_options" id="cpi_dropdown_options">
                <option <?php selected( get_post_meta($post->ID, 'cpi_dropdown_options', true), 'Option 1' ); ?>>No</option>
                <option <?php selected( get_post_meta($post->ID, 'cpi_dropdown_options', true), 'Option 2' ); ?>>Yes</option>
            </select>
            <br />
        </p>
        </div>
    </fieldset>
<?php
}

add_action('save_post', 'custom_add_save');

function custom_add_save($postID){

    // called after a post or page is saved
    if($parent_id = wp_is_post_revision($postID)) {
        $postID = $parent_id;
    }

    if ($_POST['cpi_dropdown_options']) {
        update_custom_meta($postID, $_POST['cpi_dropdown_options'], 'cpi_dropdown_options');
    }

    if ($_POST['cpi_text_option']) {
        update_custom_meta($postID, $_POST['cpi_text_option'], 'cpi_text_option');
    }
}

function update_custom_meta($postID, $newvalue, $field_name) {
    // To create new meta
    if(!get_post_meta($postID, $field_name)){
        add_post_meta($postID, $field_name, $newvalue);
    } else {
        // or to update existing meta
        update_post_meta($postID, $field_name, $newvalue);
    }
}
?>
share|improve this question

I cannot be 100% sure since I can't test without installing and running you code but I think the problem is that you aren't echoing your selected markup.

<option <?php selected( 
    get_post_meta($post->ID, 'cpi_dropdown_options', true), 
    'Option 1', 
    true
); ?>>No</option>

The last parameter tells selected to echo output.

Edit:

Your options aren't sending a value. Compare your code to this code from the Codex:

<option value="1" <?php selected( $options['foo'], 1 ); ?>>1</option>

Your code is missing the value= part. You claim that:

...if I create a post and set the option to Yes, then I go edit to post it shows No but in the custom meta field it does show Yes so it's saving correctly. I know that the No and Yes values should be dynamic and check if any custom meta has been set to display it but not sure what the code is.

That sounds as if you have verified that things are actually saving to the database as you want but without that value part I don't see how they could be. That is, without the value part your form is sending the option contents-- 'No', and 'Yes'. You can verify that with this little snippet:

<form>
  <select name="hi" method="get">
    <option>No</option>
    <option>Yes</option>
  </select>
  <input type="submit" value="go" />
</form>

However, you are looking for true values of "Option 1" and "Option 2". You can either add the value attribute to match what selected is looking for, which is probably best because you can then alter the outward appearance of the form without altering the function of the form, or you can check for "No" and "Yes" instead of "Option 1" and "Option 2".

share|improve this answer
    
Thanks but it's not working. If I have set the drop down to Yes, update the post and I see the custom field value as Yes. The drop down still shows No. – Anagio Jan 12 '13 at 22:00

Are you sure the output of the 'pi_dropdown_options' post_meta is actually in the form of 'Option 1' or 'Option 2'? If it's not seeing either of those values in the output of 'pi_dropdown_options' then you'd just see the first item in the dropdown selected. Have you tried just echoing out the 'pi_dropdown_options' custom field to see what it says? It will be case-sensitive matching too.

Another thing to note (as pointed out by s_ha_dum in a previous answer) is the the markup for the 'option' elements in your list are not setting a value:

<option<?php selected( get_post_meta($post->ID, 'cpi_dropdown_options', true), 'Option 1' ); ?>>No</option>

If I read that correctly it looks like if this was the selected option the markup would be:

<option selected="selected">No</option>

So, if you then saved the post the 'pi_dropdown_options' post_meta would likely be empty, it should have a 'value' attribute on each option like:

<option value="Option 1" selected>No</option>

So the PHP code would be something like:

<option value="Option 1" <?php selected( get_post_meta($post->ID, 'cpi_dropdown_options', true), 'Option 1' ); ?>>No</option>

Not sure if any of that helps! Alternatively you could try the alternative syntax for checking option selected state without using the 'selected' WordPress function:

<option value="Option 1" <?php if ( get_post_meta($post->ID, 'cpi_dropdown_options', true) == 'Option 1' ) echo 'selected="selected"'; ?>>No</option>
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.