Custom Query – List Users by last name
-
I have a select menu for a search function populated by a custom query ordered by user last name that is working fine but I have a clunky exclude to stop the admin user being listed:
<select name="author" id="select-author"> <option value="">Select Author</option> <?php $excluded = "1"; //exclude users by ID $authors = $wpdb->get_results("SELECT * FROM $wpdb->users INNER JOIN $wpdb->usermeta ON ($wpdb->users.ID = $wpdb->usermeta.user_id) WHERE $wpdb->usermeta.meta_key = 'last_name' AND $wpdb->usermeta.user_id NOT IN ($excluded) ORDER BY $wpdb->usermeta.meta_value ASC"); foreach($authors as $author): $select .='<option value="'. $author -> user_nicename .'">' . $author -> display_name . '</option>'; endforeach; print $select; ?> </select>
At some point I’m sure we are going to have more users and I don’t want to be excluding them based on their ID but I can make do by limiting the users to all those with the author role. I don’t see user_role currently listed in the resulting array for $authors. What I’d like to do is to add to the query and instead of excluding user ID’s have only users with the role of author in the results. What’s the best way to do this? I’ve tried a few things but nothing works so far……
Thanks!
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Custom Query – List Users by last name’ is closed to new replies.