79391965

Date: 2025-01-27 19:38:05
Score: 0.5
Natty:
Report link

Just thought I'd post my solution, I'm thinking it's verbose, but it works. I'll gladly receive any tips on how to reduce the bloat.

I included a little foreach loop which builds a list of IDs from usernames as I feel this would make more sense to anyone looking at this in future.

<?php
// Removes specified users
add_action( 'bp_ajax_querystring', 'user_remove_mem_directory', 20, 2 );
function user_remove_mem_directory( $query, $object ) {
    if ( 'members' != $object ) {
        return $query;
    }
    $usernames = array('Admin1', 'Admin2');
    foreach ($usernames as $username) {
        $user = get_user_by('login', $username);
        $excluded_users[] = $user->ID;
    } // Build array of user IDs from login usernames
    $args = wp_parse_args( $query ); // Merges user query into array.
    if ( ! empty( $args['exclude'] ) ) {
        $args['exclude'] = $args['exclude'];
        foreach ($excluded_users as &$id) {
             $args['exclude'] .= ',' . $id; // Loop through building an array
        }
    } else {
        $args['exclude'] = $excluded_users;
    }
    $query = build_query( $args );
    return $query;
}
Reasons:
  • Blacklisted phrase (1): any tips
  • Long answer (-1):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: morgyface