Custom Fields
Re-Envisioned
Support MB User Profile rwmb_my_custom_field_after_save_field can't update my own fields in the database

This topic contains 2 replies, has 2 voices, and was last updated by  proyectohappyweb@gmail.com 2 months, 3 weeks ago.

  • Creator
    Topic
  • #14264
    Resolved
    proyectohappyweb@gmail.com
    Participant

    Hello,

    I’m using metabox type user with the custom table users.

    I want to use this action:

    add_action( 'rwmb_my_custom_field_A_after_save_field', 'usuario_aprobado_con_codigo_acceso' , 20, 5 );
    function usuario_aprobado_con_codigo_acceso ( $null, $field, $new, $old, $post_id ) {

    Depending of the field my_custom_field_A (the value is in $new), I want to update another custom field of the custom table users, my_custom_field_B

    I see in another posts
    https://metabox.io/support/topic/using-custom-attributes-from-rwmb_before_save_post-action/
    something like this:

    $storage = $field['storage'];
    $storage->update( $post_id, 'last_name', "blablabla" );

    And it run well, it update the filed last_name. It run well with the populate user fields.

    But if I want to update another custom fields that I added in this table, I don’t know what I have to do to update the value…
    I try:

    $storage->update( $post_id, 'my_custom_field_B', "blablabla" );

    don’t work…
    I try a query:

        global $wpdb;
        $wpdb->update('usuarios', array( 'my_custom_field_B' => 'blablabla'), array( 'ID' => $post_id ) ); 

    and don’t work…

    Do you know what I’m doing wrong?

    Thanks,
    Sergio

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #14267

    Anh Tran
    Keymaster

    Hi Sergio,

    Did you try:

    add_action( 'rwmb_field_A_after_save_field', function ( $null, $field, $new, $old, $object_id ) {
        $storage = $field['storage'];
        $storage->update( $object_id, 'field_B', 'new value' );
    }, 10, 5 );

    This code works only when the field A is put after field B in the meta box, which means field B is already saved.

    If field A is before field B, then after we run this code, field B still use value from the submission. In this case, please try this code:

    add_action( 'rwmb_field_A_after_save_field', function ( $null, $field, $new, $old, $object_id ) {
        $_POST['field_B'] = 'new value'; // Modify directly submitted value from $_POST.
    }, 10, 5 );
    #14283

    Yes, I try the first method but don’t run.

    But with the second:
    $_POST[‘field_B’] = ‘new value’;
    It run well!!!

    Thanks once again.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.