aamplugin / advanced-access-manager

WordPress Advanced Access Manager Plugin
Other
31 stars 14 forks source link

User level filtering only in administration #200

Open Adipso-Alexis opened 2 years ago

Adipso-Alexis commented 2 years ago

Hi,

Like your example ( https://aamplugin.com/article/wordpress-users-and-roles-filter ), I want Editors can manage only themselves and Project Managers can manage Editors but should never be able to manage or promote anybody to the Administrator role.

I created a hierarchy on my site with level_XX capabilities and enable the user level filter option. It works fine in administration, but in front I have to make a list of users and I have to display an ACF field where you can select a user. When the user level filter option is enable get_users() and the ACF field return an empty array when I check the site like a visitor and only editors when I'm logged as an editor. The user level filter works in front but I want user filtering only in administration.

I found a quick work-around by add a pre_get_users action that override yours and add a condition like that

add_action( 'pre_get_users', array($this, 'adi_pre_get_users'), 9999 );
public function adi_pre_get_users( $query )
{
    if ( ! is_admin() ) {
        $query->query_vars['role__not_in'] = array();
    }
}

Have you already thought to add option to choose to enable or not user level filtering on front or only in administration ?

Best regards, -Alexis

SauBaer commented 2 years ago

I second this. Just run into the same problem. Have to restrict roles in the backend but my authors need to be visible on the front end. Unfortunately I don't know what to do with the above code :(

In case @Adipso-Alexis is still arround, I'm interested where you've placed your code exactly. Did you add it to the AAM plugin or another file? Would it be possible to use it in an AAM Access Policy for visitors? I'm obviously not a coder, but I'm able to copy and paste code to a file (and do a backup beforehand).

Adipso-Alexis commented 2 years ago

Hi @SauBaer,

You can add this code in the functions.php file of your theme

SauBaer commented 2 years ago

Thank you very much @Adipso-Alexis, realy appreciate your feedback! Unfortunately I get a critical wordpress error with the code added to functions.php, I'll check with a coder friend, maybe he knows how to adjust it for my case.