admin管理员组

文章数量:1134597

I have a number custom field with a selector, I should select the number of bedrooms, when I edit the post the meta key value is saved correctly (I can see the number of bedrooms near the correspondent icon on the post) but if I reload the post editor page...the selection has disappeared, how can I fix the following code?

add_action( 'add_meta_boxes_post', "camere_add_meta_box" );
function camere_add_meta_box(){
    add_meta_box(
        "camere_meta_box",  // An ID for the Meta Box.  Make it unique to your plugin
        __( "Numero camere da letto", 'textdomain' ),  // The title for your Meta Box
        "camere_meta_box_render",  // A callback function to fill the Meta Box with the desired UI in the editor
        "post",  // The screen name - in this case the name of our custom post type
        "side"  // The context or placement for the meta box.  Choose "normal", "side" or "advanced"
    );
}
function camere_meta_box_render( $post ){
    $number = get_post_meta( $post->ID, "function_camere", true );
    echo '
    <div>
        <select name="function_camere">';
            for( $i = 0; $i < 13; $i++ ) {
                echo '<option value="'.$i.'">'.$i.'</option>';
            }
        echo '
        </select>
    </div>';
}
    
add_action( 'save_post', 'camere_meta_box_save');
function camere_meta_box_save( $post_id ) {
    if ($_POST['post_type'] == "post") {
        $number = $_POST['function_camere'];
        update_post_meta( $post_id, "function_camere", $number );
    }     
}

I have a number custom field with a selector, I should select the number of bedrooms, when I edit the post the meta key value is saved correctly (I can see the number of bedrooms near the correspondent icon on the post) but if I reload the post editor page...the selection has disappeared, how can I fix the following code?

add_action( 'add_meta_boxes_post', "camere_add_meta_box" );
function camere_add_meta_box(){
    add_meta_box(
        "camere_meta_box",  // An ID for the Meta Box.  Make it unique to your plugin
        __( "Numero camere da letto", 'textdomain' ),  // The title for your Meta Box
        "camere_meta_box_render",  // A callback function to fill the Meta Box with the desired UI in the editor
        "post",  // The screen name - in this case the name of our custom post type
        "side"  // The context or placement for the meta box.  Choose "normal", "side" or "advanced"
    );
}
function camere_meta_box_render( $post ){
    $number = get_post_meta( $post->ID, "function_camere", true );
    echo '
    <div>
        <select name="function_camere">';
            for( $i = 0; $i < 13; $i++ ) {
                echo '<option value="'.$i.'">'.$i.'</option>';
            }
        echo '
        </select>
    </div>';
}
    
add_action( 'save_post', 'camere_meta_box_save');
function camere_meta_box_save( $post_id ) {
    if ($_POST['post_type'] == "post") {
        $number = $_POST['function_camere'];
        update_post_meta( $post_id, "function_camere", $number );
    }     
}
Share Improve this question edited Aug 31, 2023 at 4:22 YourManDan 4342 silver badges12 bronze badges asked Aug 21, 2023 at 13:22 Andrea SacconiAndrea Sacconi 797 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

There's nothing in your code that would allow this to happen. In your HTML you need to add a selected attribute to the <option> element representing the current value. WordPress includes a helper function, selected() which can help with this:

function camere_meta_box_render( $post ){
    $number = get_post_meta( $post->ID, "function_camere", true );
    echo '<div><select name="function_camere">';
    for( $i = 0; $i < 13; $i++ ) {
    echo '<option value="'.$i.'" ' . selected( $i, $number, false ) . '>'.$i.'</option>';
    }
    echo '</select></div>';
}

本文标签: Why the value of the selector doesnt remain visible in the custom field after I edit the post