Skip Navigation

[Resolved] Issue using update_post_meta to edit post fields in custom code

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 1 replies, has 1 voice.

Last updated by davidL-7 4 weeks, 1 day ago.

Assisted by: Minesh.

Author
Posts
#2807603
Screenshot 2025-05-02 at 12.15.38 PM.png

Hi! I have a custom post type that has three custom fields (account id, account number, account name) that each need to get updated by a select field whose options contain all three values combined together (and separated by a | character).

See the screenshot for a typical select option that contains the three values in the value attribute.

When the form is submitted, it triggers custom code that explodes the value into an array of three items, then uses update_post_meta, which is intended to update each of the three custom field values, like so:

add_action('cred_save_data', 'tssupp_auto_connect_to_parent_4',99,2);
function tssupp_auto_connect_to_parent_4($post_id, $form_data) {
if (in_array($form_data['id'], array( 30106 ))) { // Edit Aid Request (ID: 30106)
global $wpdb;
$account_params = $_POST['aid-account-array'];
$account_params_explode = explode('|', $account_params);
update_post_meta( $post_id, 'wpcf-account-name', $account_params_explode[2], true );
update_post_meta( $post_id, 'wpcf-aid-account-id', $account_params_explode[0], true );
update_post_meta( $post_id, 'wpcf-aid-account-number', $account_params_explode[1], true );
}
}

There's no error that I can see, but the fields in the custom post aren't getting updated.

One has to be logged in to see the work in progress (Minesh should have access to this site). One logged in, the edit form working on post id 40024 can be accessed via this URL:

hidden link

The Aid Account selection should be changed (other fields can be edited too, but it's the Aid Account we're concerned with).

When "Update" is pressed, you'll be redirected to hidden link, where one should see the new Account selected (but the change isn't being made).

The values for the Account ID, Account Number, and Account Name fields for post 40024 can be checked at hidden link

I assume my problem is in the custom code, but I'm not sure what I'm doing wrong.

Thanks for the assist,
David

#2807768

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Please note that once ticket is marked resolve private access details will be automatically deleted by system. In order to review your code I will require admin access details.

Can you please send me admin access details.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2808024

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Can you please check now.

I've adjusted the code you added to "Custom Code" section with the code snippet "redirect_edit_aid_request_to_review_aid_request" as given under:

add_action('cred_save_data', 'tssupp_auto_connect_to_parent_4',99,2);
function tssupp_auto_connect_to_parent_4($post_id, $form_data) {
 
      if (in_array($form_data['id'], array( 30106 ))) { // Edit Aid Request  (ID: 30106)
 
        global $wpdb;

      	$account_params = $_POST['aid-account-array'];
        $account_params_explode = explode('|', $account_params);
      
        
      	update_post_meta( $post_id, 'wpcf-account-name', $account_params_explode[2]);
      	update_post_meta( $post_id, 'wpcf-aid-account-id', $account_params_explode[0] );
      	update_post_meta( $post_id, 'wpcf-aid-account-number', $account_params_explode[1]);
       
    }

}

Can you please confirm it works as expected.

#2808063

Thanks Minesh, confirming that it now works as expected. When I compare your change, the only difference I can see is that you dropped the "true" parameter from the update_post_meta function. If you did something else that I'm missing, please let me know... I'm running into the same bug in other contexts, so I'll try to fix it myself and open another ticket if I get stuck. Thanks again!