Home > database >  Add_Meta_Box parameter not working / Saving values
Add_Meta_Box parameter not working / Saving values

Time:06-20

I'm adding a meta box to a CPT. The meta box appears in both Post and CPT types, however, the Context Parameter "side" is not working for the CPT. It works only for the Post. I am trying to show the metabox in the sidebar, but currently the CPT (ref: works) only displays it in the post editor's footer.

Also, is there a name for the screen that lists all posts so that the user is able to toggle the metadata in quick edit?

Other fault found. Values are not saving.

Edit: my local Php was outdated for the parameter issue, however the save values thing is still not working

    global $post;
    $screens = ["works", "post"]; 
    foreach ( $screens as $screen ) {
        add_meta_box(
            'book-status',                 // Unique ID
            'Book Status',       // Box title
           'custom_box_html', 
            // Content callback, must be of type callable
            $screen,// Post type
            'side'
        );
    }
}

add_action( 'add_meta_boxes',  'add_custom_box' );

 function custom_box_html( $post ) {
     $value = get_post_meta( $post->ID, '_wporg_meta_key', true );
    ?>  
    <label for="typechild2022-field"> 
        <?=esc_html_e('Select Book Status', 'typechild2022'); ?> 
    </label>
    <select name="typechild2022_field" id="typechild2022-field" > 
       <option value="Select" <?= selected($value, "Select"); ?> >
            <?= esc_html_e("Select", 'typechild2022'); ?>
                
        </option>
       <option value="Published" <?= selected($value, "Published"); ?> >
            <?= esc_html_e("Published", 'typechild2022'); ?>
                
        </option>
        <option value="In-Progress" <?php selected($value, "In Progress"); ?> >
        <?= esc_html_e("In Progress", 'typechild2022'); ?>
               </option>
                </select>
    <?php
}

function wporg_save_postdata( $post_id ) {
    if ( array_key_exists( 'typechild2022-field', $_POST ) ) {
        update_post_meta(
            $post_id,
            '_wporg_meta_key',
            $_POST['typechild2022_field']
        );
    }
}
add_action( 'save_post', 'wporg_save_postdata' );

CodePudding user response:

I forgot to add an add action to save it... still no idea how to add to quick edit though

    if ( array_key_exists( 'typechild2022_field', $_POST ) ) { // array key value = html <select name>
        update_post_meta(
            $post_id,
            'book-status',
            $_POST['typechild2022_field'] // array key value = html <select name>
        );
    }
} 

add_action('save_post', 'wporg_save_postdata', 25, 1);```
  • Related