Hello! I need to display the list of users currently logged on the site, except for the user with user_id = 1 and user_name = guest. Tell me how to do it. I display a list of users with this code:

<?php $users_online = $wpdb->get_results("SELECT user_name, user_id FROM wp_useronline"); // Echo the title of the first scheduled post if ($users_online) { foreach ($users_online as $user_online) { echo $user_online->user_name . "\n"; } } ?> 

I can not correctly make a condition to filter out user_id 1 and user_name Guest.

    1 answer 1

    Filter users better when selecting from the database, specifying the condition

     SELECT user_name, user_id FROM wp_useronline WHERE user_id != 1 AND user_name != 'guest' 

    If for some reason you do not want to change the sql query, then in the loop specify the condition

     foreach ($users_online as $user_online) { if($user_online->user_id != 1 and $user_online->user_name != 'guest') echo $user_online->user_name . "\n"; }