Home › Forums › Feature Requests › Restrict Relationship Fields Choices to Current User › Reply To: Restrict Relationship Fields Choices to Current User
Hi folk,
I had the quite similar issue. I needed also to grant a full access to “editor” & “administrator”.
Here is the code inserted in the functions.php
file
function modify_field( $args, $field,$post) {
if( !check_user_role(array('editor','administrator')) ){
$args['author'] = get_current_user_id();
}
return $args;
}
add_filter( 'acf/fields/post_object/query/key=field_XXX..XXX', 'modify_field', 10, 3 );
/* @src & @credits https://wp-mix.com/wordpress-check-user-roles/ */
function check_user_role($roles, $user_id = null) {
if ($user_id) $user = get_userdata($user_id);
else $user = wp_get_current_user();
if (empty($user)) return false;
foreach ($user->roles as $role) {
if (in_array($role, $roles)) {
return true;
}
}
return false;
}
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.