wp code snippet

WordPress, PHP quick help codes

Custom SQL query to get WordPress user lists

Sometime we need to write a custom SQL query to fetch users. The $wpdb object can be used to run queries against the WordPress database. Here is an example of it.

global $wpdb;
$authors = $wpdb->get_results("SELECT user_id from $wpdb->usermeta WHERE meta_key = 'last_name' ORDER BY meta_value");
foreach ($authors as $author) {
$user = get_userdata($author->user_id);
print_r($user);
}

Categories: PHP, WordPress

Leave a Reply

%d bloggers like this: