New Posts are not added to table
Support › MB Custom Table › New Posts are not added to tableResolved
- This topic has 4 replies, 2 voices, and was last updated 6 years, 6 months ago by
toddmckee.
-
AuthorPosts
-
October 24, 2018 at 12:40 AM #11730
toddmckee
ParticipantI'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,
-ToddOctober 24, 2018 at 2:40 AM #11731toddmckee
ParticipantI 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,
-ToddOctober 24, 2018 at 11:12 PM #11739toddmckee
ParticipantI'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,
-ToddOctober 25, 2018 at 5:02 PM #11756Anh Tran
KeymasterHi 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.
October 25, 2018 at 9:21 PM #11760toddmckee
ParticipantAnh,
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.
-
AuthorPosts
- You must be logged in to reply to this topic.