Make WordPress Core


Ignore:
Timestamp:
02/23/2016 04:39:50 PM (9 years ago)
Author:
boonebgorges
Message:

Query: Allow a seed value to be passed when using 'rand' $orderby.

WP_Query allows random ordering; 'orderby' => 'rand' translates to
ORDER BY RAND(). This syntax results in random ordering that is not
consistent from request to request. MySQL supports the passing of a seed value
to random sorts, such as ORDER BY RAND(3), which will return the same
random value each time it's called. WP_Query now supports this syntax, by
passing RAND(3) (or whatever integer seed value you'd like) as the value
of 'orderby'.

Props hlashbrooke.
Fixes #35692.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/post/query.php

    r35242 r36632  
    305305
    306306    /**
     307     * @ticket 35692
     308     */
     309    public function test_orderby_rand_with_seed() {
     310        $q = new WP_Query( array(
     311            'orderby' => 'RAND(5)',
     312        ) );
     313
     314        $this->assertContains( 'ORDER BY RAND(5)', $q->request );
     315    }
     316
     317    /**
     318     * @ticket 35692
     319     */
     320    public function test_orderby_rand_should_ignore_invalid_seed() {
     321        $q = new WP_Query( array(
     322            'orderby' => 'RAND(foo)',
     323        ) );
     324
     325        $this->assertNotContains( 'ORDER BY RAND', $q->request );
     326    }
     327
     328    /**
     329     * @ticket 35692
     330     */
     331    public function test_orderby_rand_with_seed_should_be_case_insensitive() {
     332        $q = new WP_Query( array(
     333            'orderby' => 'rand(5)',
     334        ) );
     335
     336        $this->assertContains( 'ORDER BY RAND(5)', $q->request );
     337    }
     338
     339    /**
    307340     * Tests the post_name__in attribute of WP_Query.
    308341     *
Note: See TracChangeset for help on using the changeset viewer.