Support

Account

Home Forums ACF PRO Auto populate custom field with user data Reply To: Auto populate custom field with user data

  • If this is only going to be for logged in users, the I would probably use a user field rather than a text field so that the post can be associated with the user ID. I’d also only show it in the admin, since you’re auto populating it with the current user it should not be something that user could change.

    To not show the field on the front end I would create an acf/prepare_field filter https://www.advancedcustomfields.com/resources/acf-prepare_field/

    
    add_filter('acf/prepare_field/name=name_provider', 'only_show_name_provider_in_admin');
    function only_show_name_provider_in_admin($field) {
      if (is_admin()) {
        return $field;
      }
      return false;
    }
    

    Then I wold use an acf/save_post filter https://www.advancedcustomfields.com/resources/acf-save_post/ to update this field after the post was saved

    
    add_action('acf/save_post', 'update_name_provider_field');
    function update_name_provider_field($post_id) {
      if (is_admin()) {
        // don't run in admin
        return;
      }
      if (get_post_type($post_id) != 'YOUR CUSTOM POST TYPE')) {
        // don't run if not your post type
        return;
      }
      update_field('name_provider', get_current_user_id(), $post_id);
    }