• Hi there, I’m running into some problems with User_query. For some reason the attached code isn’t working.

    The issue is that $users is empty but not sure what I’m doing wrong to return a query with all of the site’s current users.

            // Create the WP_User_Query object
            $user_query = new WP_User_Query(array());
    
            $users = $user_query->results;
           
            // loop through each user
            foreach ($users as $user) {
    
                $id = sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
    
                // add points meta all the user's data
                add_user_meta($user->id, 'pitch_id', $id, false);
            }
            
        }

    As always, any help very much appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter zagreus

    (@zagreus)

    eta I replaced the WP_User_Query with get_users and it worked. However, I would love to know what the issue is with the Query object above if anyone knows ..

    Moderator bcworkz

    (@bcworkz)

    $user_query->results is a private property, you cannot access it directly. Use $user_query->get_results(). Silly OOP games ??

    To be fair, there can be very valid reasons to require going through a method to get a property. Just not in this case. Maybe it makes more sense in classes extended from WP_User_Query.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘User_Query problem’ is closed to new replies.