1

I've created two custom post types, buildings and availabilities, and added a query on availabilities to select what building it belongs too but I can't seem to get it to save.

add_action('admin_init', 'p2p2_add_building_metabox');

function p2p2_add_building_metabox(){
add_meta_box( 
    'building_availability', 
    __('Building', 'bandpress'), 
    'p2p2_building_availability_metabox', 
    'availabilities', 
    'side', 
    'default', 
    array( 'id' => 'p2p2_building') 
);}

function p2p2_building_availability_metabox($post, $args) {
    wp_nonce_field( plugin_basename( __FILE__ ), 'p2p2_building_nonce' );
    $building_id = get_post_meta($post->ID, 'p2p2_building', true);

    echo "<p>Select The Building of This Availability</p>";
    echo "<select id='p2p2_building' name='p2p2_building'>";

    // Query the buildings here
    $query = new WP_Query( 'post_type=building' );
    while ( $query->have_posts() ) {
        $query->the_post();
        $id = get_the_ID();
        $selected = "";

        if ($id == $author_id) {
            $selected = ' selected="selected"';
        }
        echo '<option' . $selected . ' value=' . $id . '>' . get_the_title() . '</option>';
    }

    echo "</select>";
}
 
add_action('save_post', 'p2p2_save_building_metabox', 1, 2);
function p2p2_save_building_metabox($post_id, $post){
    // Don't wanna save this now, right?
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    
    if ( !isset( $_POST['p2p2_building_nonce'] ) )
        return;

    if ( !wp_verify_nonce( $_POST['p2p2_building_nonce'], plugin_basename( __FILE__ ) ) )
        return;

    // We do want to save? Ok!
    $key = 'p2p2_building';
    $value = $_POST["p2p2_building"];
    if ( get_post_meta( $post->ID, $key, FALSE ) ) { // If the custom field already has a value
        update_post_meta( $post->ID, $key, $value );
    } else { // If the custom field doesn't have a value
        add_post_meta( $post->ID, $key, $value );
    }

    if ( !$value )
        delete_post_meta( $post->ID, $key ); // Delete if blank
}

1 Answer 1

0

It seems you have one error in your code...

if($id == $author_id){
    $selected = ' selected="selected"';
}

It should be $building_id instead of $author_id.
I tested the rest of the code and the saving is working fine.

If you had printed/echoed out the value of this line ...

$building_id = get_post_meta($post->ID, 'p2p2_building', true);

like print_r($building_id);, you would had seen, that the correct post-ID was already saved. So only your select-element was not able to set the "selected" attribute correctly.

P.S. Maybe you also want to add an empty default option to the select field. As sometimes a user wont fill out this field, and than the first options post-ID gets always saved.

Something like...

echo "<select id='p2p2_building' name='p2p2_building'>";
// empty default option:
echo "<option value="">Select building...</option>";
1
  • Thank you! That fixed the issue and got it working!
    – alexhailey
    Commented Dec 15, 2020 at 14:21

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.