[wp-hackers] Seeking SQL advice to identify performance problem

Mike Walsh mpwalsh8 at gmail.com
Wed Jun 20 15:06:42 UTC 2012


I must be missing something as I am unable to retrieve the first_name and
last_name fileds using get_users().  Here is an example (this is dummy
data, not from a real user) of what I get back when I call get_users() as
outlined below:

[885] => WP_User Object        (            [data] => stdClass Object
              (                    [ID] => 885
[user_login] => aarntzen                    [user_pass] =>
$P$BFVck6WesTLX9SboEeNAaaaDxZQfx81                    [user_nicename]
=> aarntzen                    [user_email] =>
aarntzen at wpst.example.com                    [user_url] =>
        [user_registered] => 2012-06-19 14:53:28
[user_activation_key] =>                     [user_status] => 0
            [display_name] => Alejandro Arntzen                )
     [ID] => 885            [caps] => Array                (
         [subscriber] => 1                )            [cap_key] =>
wp_capabilities            [roles] => Array                (
         [0] => subscriber                )            [allcaps] =>
Array                (                    [read] => 1
  [level_0] => 1                    [subscriber] => 1                )
           [filter] =>         )

This is how I called it:

    $args = array(
        'fields' => 'all_with_meta'
       ,'exclude' => $exclude_id
       ,'number' => 25
    ) ;
$users = get_users($args) ;

Am I missing something?

Mike

On Wed, Jun 20, 2012 at 2:52 PM, Simon Blackbourn <piemanek at gmail.com>wrote:

> >
> >
> > > I have a query that I have used in a couple plugins that someone on
> this
> > > mailing list helped me with several years ago.  I want to retrieve the
> > list
> > > of Users, including their ID, username, display name, first name, and
> > last
> > > name so I can present the information in a form in a way that is easily
> > > readable.
> >
>
>
> You can use the get_users() function [1] with the 'fields' parameter set
> to 'all_with_meta', and then perform a usort on the results:
>
> $args = array(
> 'number'     => 1000000, //  unfortunately you can't use -1 here
> 'fields'     => 'all_with_meta', // required
>                        'exclude' => array( 1 ) // user IDs to exclude
> );
>
> $user_list = get_users( $args );
>
> if ( ! empty( $user_list ) ) {
> usort( $user_list, 'sort_my_users_by_lastname' );
> foreach ( $user_list as $user ) {
>                // do stuff here...
> }
> }
>
> // .....
>
> function  sort_my_users_by_lastname( $a, $b ) {
>
> if ( $a->last_name == $b->last_name ) {
> return 0;
> }
>
> return ( $a->last_name < $b->last_name ) ? -1 : 1;
>
> }
>
>
> Ideally you would paginate using the 'number' and 'offset' fields to make
> it more efficient, but if you need all users in one go as I did, sorted by
> lastname, it still seems to work very well. I've got this running on a site
> without just short of 1000 users and the query takes 1/100th of a second.
>
> Simon
>
> [1] http://codex.wordpress.org/Function_Reference/get_users
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>



-- 
Mike Walsh - mpwalsh8 at gmail.com


More information about the wp-hackers mailing list