Search code examples
wordpresswordpress-themingcustom-wordpress-pages

Wordpress meta box not saving and updating


function wporg_add_custom_box(){
    $screens = ['product', 'wporg_cpt'];
    foreach ($screens as $screen) {
        add_meta_box(
            'wporg_box_id',           // Unique ID
            'Select a Authors Name ',  // Box title
            'wporg_custom_box_html',  // Content callback, must be of type callable
            $screen                   // Post type
        );
    }
}
add_action('edit_form_after_title', 'wporg_add_custom_box');

function wporg_custom_box_html($post){
    $value = get_post_meta($post->ID, '_getwriter_id', true);
    ?>
    <select name="book_writer_id" id="book_writer_id" class="postbox">
        <?php
$type = 'book_authors'; // your post type
$args=array(
  'post_type' => $type,
  'post_status' => 'publish',
  'posts_per_page' => -1,
  'caller_get_posts'=> 1
);
$my_query = null;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
  while ($my_query->have_posts()) : $my_query->the_post();  $id = get_the_ID();?>
     <option value="<?=$id; ?>" <?php selected($value, $id); ?>><?=the_title(); ?></option>
    <?php
  endwhile;
}
wp_reset_query();  // Restore global post data stomped by the_post().
?>
    </select>
    <?php } 
function wporg_save_postdata($post_id)
{
    if (array_key_exists('book_writer_id', $_POST)) {
        update_post_meta(
            $post_id,
            '_getwriter_id',
            $_POST['book_writer_id']
        );
    }
}
add_action('save_post', 'wporg_save_postdata');

Solution

  • I did not used there save_postdate function that why, it not saving value and updating data

    function wporg_add_custom_box(){
        $screens = ['product', 'wporg_cpt'];
        foreach ($screens as $screen) {
            add_meta_box(
                'wporg_box_id',           // Unique ID
                'Select a Authors Name ',  // Box title
                'wporg_custom_box_html',  // Content callback, must be of type callable
                $screen                   // Post type
            );
        }
    }
    add_action('edit_form_after_title', 'wporg_add_custom_box');
    
    function wporg_custom_box_html($post)
    {
        $value = get_post_meta($post->ID, '_getwriter_id', true);
        ?>
        <select name="book_writer_id" id="book_writer_id" class="postbox">
            <?php
    $type = 'book_authors'; // your post type
    $args=array(
      'post_type' => $type,
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();  $id = get_the_ID();?>
         <option value="<?=$id; ?>" <?php selected($value, $id); ?>><?=the_title(); ?></option>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
        </select>
        <?php
    }
    
    function wporg_save_postdata($post_id)
    {
        if (array_key_exists('book_writer_id', $_POST)) {
            update_post_meta(
                $post_id,
                '_getwriter_id',
                $_POST['book_writer_id']
            );
        }
    }
    add_action('save_post', 'wporg_save_postdata');