0

How select users between 100 and 200 user ID's? Or select users which ID less then 200?

$args = array (
   'query'     => array(
       array(
           'key'       => 'ID',
           'value'     => '200',
           'compare'   => '<=',
       ),
   ),
);

$user_query = new WP_User_Query( $args );
  • IDs are random number that you should not assume anything about, except for uniqueness. In theory you might not have any users between 100 and 200 but only above 10000 – Mark Kaplun Apr 13 '18 at 10:20
  • Why it's random? ID field in wp_users table have attribue Auto Increment, so it's not random number. – user2481034 Apr 13 '18 at 10:22
  • because that is the definition of the user ID. The fact that default implementation will have them with auto increment, do not mean that there may never be other implementations. If you will look around here you will find questions about how to change the user ID, or make it start in 10000 – Mark Kaplun Apr 13 '18 at 10:24
  • ... and obviously users can be deleted – Mark Kaplun Apr 13 '18 at 10:26
1

Here's one way using the include with a range from 100 to 200:

$user_query = new WP_User_Query( [ 'include' => range( 100,200 ) ] );
  • This way not good because in real I need select users from ID 2000 until 11000. And SQL putting all this ID's in string and mysql crashing. Need another way. – user2481034 Apr 13 '18 at 9:43
  • Why are you fetching so many each time (many thousands), why don't you use a paging instead (fetching like a 50-100 each time)? – birgire Apr 13 '18 at 9:44
  • I need this for script, that select users what I need and change something in their profile. I may use own SELECT * from wp_users WHERE ID >= 2000 AND ID <= 11000 query, but I am interesting how do this with WP_User_Query – user2481034 Apr 13 '18 at 9:48
  • How are you updating each user? Is it run via cli or http request (timeout possible) ? It's possible to do this via filters, but I still wonder if you really need it ;-) – birgire Apr 13 '18 at 9:54
  • lol, the answer lack any elegance but it does literally answer the question :) – Mark Kaplun Apr 13 '18 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.