我也有类似的问题。我需要restrict 关系字段选择posts owned by Current User 但授予对“编辑器”的完全访问权限(&P);“管理员”。
下面是插入到functions.php
文件
<?php
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;
}
?>