Support

Account

Home Forums General Issues Set current user as default in relational select field

Solving

Set current user as default in relational select field

  • I have a relational select field that lists all user-editors. I want the default selection to be the current user. I don’t see an option for a default value in this scenario. How do I set the default value for this?

  • Hi @pweil

    Do you mean that you have a user field listing all editors? Or is it a select field you fill dynamically with the users?

  • The latter. This is a post for recording some basic information for projects users will be managing. This info includes the name of the project manager. Often (but not always), it will be the current user, so I’d like that user’s name selected by default.

  • Ah,

    So when you fill in the users as choices in the select field I think it’s possible to set the default value as well.. Could you post the code you’re using?

  • There’s really no code to show you for this. So far, it’s all in the GUI. The select field is being used when the user is creating or editing a custom post type. The choices (users listed as editors) are being populated automatically because it’s a relational field.

    Is there something I can add to functions.php or someplace to set the default value for the editor field to the current user?

  • Ah okay.. But then the field is an User field (in ACF)? Relationship fields do not work towards users so it has to be a User field.

  • Yes, it is a User field. We’re getting tripped up by some confusing terminology. I said “Relational” because the User field type is one of several fields (Page Link, Post Object, Relationship, Taxonomy, User) listed under the “Relational” category. I hope that clarifies things.

  • Yeah thanks. I needed to be sure in what way you’re creating the field 🙂

    You should be able to set a default value of the current user using this filter:
    http://www.advancedcustomfields.com/resources/acfload_field/

    This code should work just fine! Be sure to change the field_name to your own. If you want to be safe you can also change it to use the field key instead.

    
    function user_acf_load_field( $field ){
    
        $field['value'] = get_current_user_id();
        return $field;
    }
    
    // acf/load_field - filter for every field
    add_filter('acf/load_field/name=userfield', 'user_acf_load_field');
    
  • @brendha don’t duplicate answers please.

  • This reply has been marked as private.
Viewing 10 posts - 1 through 10 (of 10 total)

The topic ‘Set current user as default in relational select field’ is closed to new replies.