Home › Forums › General Issues › 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');
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.