Support MB Custom Table New Posts are not added to table Reply To: New Posts are not added to table

#11760
toddmckeetoddmckee
Participant

Anh,

I don't know the specific reason behind the behavior, but I have noticed a couple of things that might lead to the answer. If you remove the custom table option and post everything to the postmeta table, empty values are pushed as empty serialized data "a:0{}". In the custom table, no data is pushed for empty fields. I had a similar issue with advanced select fields in the past. I had to set a default value of "a:0{}" in the database for these fields.

So my code at to check is:

add_action('rwmb_physicians_before_save_post', function( $post_id )
{
  // Create full name to store in 'physician_full_name' field
  $first_name = $_POST['physician_first_name'];
  $middle_name = $_POST['physician_middle_name'];
  $last_name = $_POST['physician_last_name'];

  $full_name = $last_name . ' ' . $first_name . ' ' . $middle_name;

  $_POST['physician_full_name'] = $full_name;

  // Get the ID of the post
  $pid = get_the_ID();

  global $wpdb;
  $table_name  = $wpdb->prefix."uams_physicians";

  // Check if the ID exists in the custom table
  $ID = $wpdb->get_var("SELECT ID FROM $table_name WHERE ID = '$pid'");
  // If the ID doesn't exist, insert a new row with the ID
  if (!$ID) {
     // Insert
     $wpdb->insert( $table_name, array(
       "ID" => get_the_ID()
      ),
      array( '%s' )
    );
  }

} );

physicians is the metabox set.