Make WordPress Core

Changeset 34531


Ignore:
Timestamp:
09/25/2015 04:25:20 AM (9 years ago)
Author:
boonebgorges
Message:

Introduce 'paged' parameter for WP_User_Query.

This is an alternative to using 'offset', and manually calculating pagination.
Note that 'paged' works only in conjunction with 'number', the latter of which
provides the per-page value.

Props sebastian.pisula.
Fixes #25145.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-user-query.php

    r34393 r34531  
    8484     *              for `$orderby` parameter.
    8585     * @since 4.3.0 Added 'has_published_posts' parameter.
     86     * @since 4.4.0 Added 'paged' parameter.
    8687     * @access public
    8788     *
     
    125126     *                                             conjunction with pagination. Value -1 (all) is not supported.
    126127     *                                             Default empty (all users).
     128     *     @type int          $paged               When used with number, defines the page of results to return.
     129     *                                             Default 1.
    127130     *     @type bool         $count_total         Whether to count the total number of users found. If pagination
    128131     *                                             is not needed, setting this to false can improve performance.
     
    158161                'offset' => '',
    159162                'number' => '',
     163                'paged' => 1,
    160164                'count_total' => true,
    161165                'fields' => 'all',
     
    324328        // limit
    325329        if ( isset( $qv['number'] ) && $qv['number'] ) {
    326             if ( $qv['offset'] )
     330            if ( $qv['offset'] ) {
    327331                $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
    328             else
    329                 $this->query_limit = $wpdb->prepare("LIMIT %d", $qv['number']);
     332            } else {
     333                $this->query_limit = $wpdb->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
     334            }
    330335        }
    331336
  • trunk/tests/phpunit/tests/user/query.php

    r32713 r34531  
    460460        $query->prepare_query( array( 'number' => 8 ) );
    461461        $this->assertNotEmpty( $query->query_limit );
    462         $this->assertEquals( 'LIMIT 8', $query->query_limit );
     462        $this->assertEquals( 'LIMIT 0, 8', $query->query_limit );
    463463
    464464        // All values get reset
     
    858858        $this->assertEqualSets( $expected, $found );
    859859    }
     860
     861    /**
     862     * @ticket 25145
     863     */
     864    public function test_paged() {
     865        $users = $this->factory->user->create_many( 5 );
     866
     867        $q = new WP_User_Query( array(
     868            'number' => 2,
     869            'paged' => 2,
     870            'orderby' => 'ID',
     871            'order' => 'DESC', // Avoid funkiness with user 1.
     872            'fields' => 'ids',
     873        ) );
     874
     875        $this->assertEquals( array( $users[2], $users[1] ), $q->results );
     876    }
    860877}
Note: See TracChangeset for help on using the changeset viewer.