Pre_get_posts only show posts by administrator roles

There’s quite a few issues with this code:

  1. $query is not defined in your callback function.
  2. role is not a valid argument for a query.
  3. You’re passing the $ids variable incorrectly.
  4. To query for multiple authors you need to use author__in.
  5. You’re checking is_category() incorrectly.

For the first issue, the problem is that you’re not getting $query from anywhere. You need to accept it as a parameter in the callback function:

function remove_unvetted_authors( $query ) {
    // $query->set( 'property', 'value' );
}

If you don’t do that, $query is undefined, and trying to use ->set() on an undefined variable will result in a 500 error. This is the issue responsible for that error code. But, even if you fix that it still won’t work until you resolve the other issues.

For the second issue, you can’t query posts by author role like that. You will need to do what you’re trying in your second example and query the author ids.

For the third issue, you’re passing $ids incorrectly. It’s a variable, so shouldn’t have quotes around it:

$query->set( 'author', $ids );

That wouldn’t throw a 500 error, but it still won’t work.

The fourth issue is that you can’t query multiple authors with the author argument. You need to use author__in:

$query->set( 'author__in', $ids );

Lastly, you’re using is_category() incorrectly. The way you’ve structured it now, remove_unvetted_authors() would apply to all queries when you’re viewing a category.

This would include menus and widgets. But, as written, even that wouldn’t work correctly. This is because is_category() will not work outside a hooked function, because whether or not it is a category hasn’t been determined yet when functions.php is run.

You only want to affect the main category queries, and you only want to do it on the front-end. So you should check is_admin() and also $query->is_category().

So after fixing all those issues, your code would look like this:

function remove_unvetted_authors( $query ) {
    if ( ! is_admin() && $query->is_category() ) {
        $user_ids = get_users( [
            'role'   => 'administrator',
            'fields' => 'ID'
        ] );

        $query->set( 'author__in', $user_ids );
    }
}
add_action( 'pre_get_posts', 'remove_unvetted_authors' );