Support

Account

Home Forums General Issues Populating custom Input Field values from custom database values

Solved

Populating custom Input Field values from custom database values

  • I have a database table, lets name it custom_table. This custom_table stores values for user_id and phone_numbers that were uploaded there from an outside source. Inside my Users, I have created an input_field and named it phone_numbers. My wp_users table and my custom_table share same user_id numbers for same users (so user with id=1 inside wp_users table is the same user with id=1 inside custom_table).

    My question is, how do I pre-populate my phone_numbers input_field inside my Users section from my custom_table? That way the admins can alter the values from Users section.

    I was looking at acf/load_field but it seems like all of the examples are for select fields. Is there documentation for input field also?

  • okay so I got the values to populate using the code below

    function populate_phone($field) {
      global $wpdb; //use wp database
      $field['value'] = 'N/A'; //default value
      $current_user_id = get_current_user_id(); //get logged in user's id
      $wp_custom_user_info = $wpdb->get_results("
        SELECT * FROM custom_table 
        WHERE wp_user=" . $current_user_id
      );
      $field['value'] = $wp_custom_user_info[0]->phone_numbers;
      
      return $field;
    }
     add_filter('acf/load_field/name=phone', 'populate_phone');

    My other question still stands: How do I update that value after pressing “Save/Update” button?

  • Solved it. If anyone ever needs this here you go. My main issue was that instead of using $_POST[‘acf’][‘field_5da4981a6418e’] I used $_POST[‘acf’][‘phone_numbers’] so it wasn’t storing the values but storing NULL.

    function update_phone() {
      global $wpdb;
      $current_user_id = get_current_user_id();
    
      $new_value = $_POST['acf']['field_5da4981a6418e'];
    
      $wpdb->update(
        "custom_table",
        array(
          "phone_numbers" => $new_value,
        ),
        array( 
          'wp_user' =>  $current_user_id,
        ),
        array( 
          '%s',
        ),
        array( '%s' )
      );
    }
    add_action('acf/save_post', 'update_phone', 1);
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.