Hopefully someone can help me with this.
When you load multiple users with get_users()
(let's say 100+) WP triggers the user meta query for every single user.
This triggers 101 queries (main user query + meta query for each user).
The thing is, I only need 1 meta value from the database (capabilities for the current blog) and I see that the WP_User class doesn't use any of the other data for role and capability management.
I already have the SQL ready, no problemo. But does anyone know how to populate the WP_User class while bypassing the default meta query?
(Off course this will need to be compatible with any stuff that happens afterwards by other plugins)
See wp-includes/class-wp-user.php
Default callstack for WP_User (when passing a $wpdb->get_results()
user object):
WP_User->_construct
WP_User->init()
WP_User->for_blog()
WP_User->_init_caps()
- get_user_meta()
- update_meta_cach()
- query :-(
For completeness, my current SQL:
SELECT {$wpdb->users}.*, {$wpdb->usermeta}.meta_value FROM {$wpdb->users}
LEFT JOIN {$wpdb->usermeta} ON {$wpdb->users}.ID = {$wpdb->usermeta}.user_id
WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->get_blog_prefix()}capabilities'
ORDER BY {$wpdb->users}.display_name
Any help greatly appreciated!
meta_query
argument + a certainmeta_value
, does that mean WordPress performs 30 queries? Or 60? I didn't get it with the 100+ and 101, but I'm really curious because THAT much fewer database queries is gooood..! Because I thought it was just one query saved to a variable and then it's just "local iteration"