diff --git a/src/wp-includes/class-wp-user-query.php b/src/wp-includes/class-wp-user-query.php index bea8dba8f8..21d013ca28 100644 --- a/src/wp-includes/class-wp-user-query.php +++ b/src/wp-includes/class-wp-user-query.php @@ -83,6 +83,7 @@ class WP_User_Query { * @since 4.2.0 Added 'meta_value_num' support for `$orderby` parameter. Added multi-dimensional array syntax * for `$orderby` parameter. * @since 4.3.0 Added 'has_published_posts' parameter. + * @since 4.4.0 Added 'paged' parameter. * @access public * * @global wpdb $wpdb @@ -124,6 +125,8 @@ class WP_User_Query { * @type int $number Number of users to limit the query for. Can be used in * conjunction with pagination. Value -1 (all) is not supported. * Default empty (all users). + * @type int $paged When used with number, defines the page of results to return. + * Default 1. * @type bool $count_total Whether to count the total number of users found. If pagination * is not needed, setting this to false can improve performance. * Default true. @@ -157,6 +160,7 @@ class WP_User_Query { 'order' => 'ASC', 'offset' => '', 'number' => '', + 'paged' => 1, 'count_total' => true, 'fields' => 'all', 'who' => '', @@ -323,10 +327,11 @@ class WP_User_Query { // limit if ( isset( $qv['number'] ) && $qv['number'] ) { - if ( $qv['offset'] ) + if ( $qv['offset'] ) { $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); - else - $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']); + } else { + $this->query_limit = $wpdb->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); + } } $search = ''; diff --git a/tests/phpunit/tests/user/query.php b/tests/phpunit/tests/user/query.php index 79a6fb1a47..6bb95d5730 100644 --- a/tests/phpunit/tests/user/query.php +++ b/tests/phpunit/tests/user/query.php @@ -459,7 +459,7 @@ class Tests_User_Query extends WP_UnitTestCase { // All values get reset $query->prepare_query( array( 'number' => 8 ) ); $this->assertNotEmpty( $query->query_limit ); - $this->assertEquals( 'LIMIT 8', $query->query_limit ); + $this->assertEquals( 'LIMIT 0, 8', $query->query_limit ); // All values get reset $query->prepare_query( array( 'fields' => 'all' ) ); @@ -857,4 +857,21 @@ class Tests_User_Query extends WP_UnitTestCase { $this->assertEqualSets( $expected, $found ); } + + /** + * @ticket 25145 + */ + public function test_paged() { + $users = $this->factory->user->create_many( 5 ); + + $q = new WP_User_Query( array( + 'number' => 2, + 'paged' => 2, + 'orderby' => 'ID', + 'order' => 'DESC', // Avoid funkiness with user 1. + 'fields' => 'ids', + ) ); + + $this->assertEquals( array( $users[2], $users[1] ), $q->results ); + } }