New Posts are not added to table

Support MB Custom Table New Posts are not added to tableResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #11730
    toddmckeetoddmckee
    Participant

    I'm using a custom table to store the meta data for a custom post type. When I create a new "post", the custom table is not adding a row or saving the data. If I manually add a row, all the data is being saved correctly. This is recent. It worked fine in the past.

    Thanks,
    -Todd

    #11731
    toddmckeetoddmckee
    Participant

    I found part of the issue, but I don't know how to proceed. The issue is the groups. If I disable them, I can save the data. Is there a suggestion on how to handle the groups with a custom table.

    Thanks,
    -Todd

    #11739
    toddmckeetoddmckee
    Participant

    I've come up with a workaround / solution.

    I'm using a before_save_post action to check if the ID exists in the custom table. If not, I insert a row with the ID. This ensures the meta boxes can save normally.

    I can post the code if anyone else is interested or having a similar issue.

    Thanks,
    -Todd

    #11756
    Anh TranAnh Tran
    Keymaster

    Hi Todd,

    Can you post your solution here? It might help others.

    I'm also interested in the problem with groups. Basically, it acts like a normal text field, the data should be saved as a serialized string.

    #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.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.