1

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 );
4
  • 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 Commented Apr 13, 2018 at 10:20
  • Why it's random? ID field in wp_users table have attribue Auto Increment, so it's not random number. Commented Apr 13, 2018 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 Commented Apr 13, 2018 at 10:24
  • ... and obviously users can be deleted Commented Apr 13, 2018 at 10:26

2 Answers 2

2

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

$user_query = new WP_User_Query( [ 'include' => range( 100,200 ) ] );
5
  • 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. Commented Apr 13, 2018 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
    Commented Apr 13, 2018 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 Commented Apr 13, 2018 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
    Commented Apr 13, 2018 at 9:54
  • lol, the answer lack any elegance but it does literally answer the question :) Commented Apr 13, 2018 at 10:21
0

If you are trying to fetch a large range it is probably good to do this:

global $wpdb;

$users = $wpdb->get_results(
    $wpdb->prepare(
        "SELECT * FROM {$wpdb->users} WHERE ID > %d AND < %d",
        100,
        10000000
    )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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