[wp-trac] [WordPress Trac] #20606: orderby rand param does not work with either WP_User_Query or get_users

WordPress Trac wp-trac at lists.automattic.com
Tue May 15 19:20:21 UTC 2012


#20606: orderby rand param does not work with either WP_User_Query or get_users
-----------------------------+------------------------------
 Reporter:  kjmeath          |       Owner:
     Type:  feature request  |      Status:  new
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  Users            |     Version:  3.2.1
 Severity:  normal           |  Resolution:
 Keywords:                   |
-----------------------------+------------------------------
Changes (by kurtpayne):

 * cc: kpayne@… (added)


Comment:

 The `WP_User_Query::prepare_query()` method limits the fields you can
 order by.  You can, however, edit the SQL ORDER BY clause directly by
 hooking `pre_user_query` action.

 {{{
 #!php
 <?php

 // Create a function to override the ORDER BY clause
 $randomize_func = create_function( '&$query', '$query->query_orderby =
 "ORDER BY RAND()";' );

 // Hook pre_user_query
 add_action( 'pre_user_query', $randomize_func );

 // Make the call
 $contributors = get_users( array( 'role' => 'subscriber', 'orderby' =>
 'rand', 'number' => 3 ) );

 // Remove the hook
 remove_action( 'pre_user_query', $randomize_func );
 }}}

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/20606#comment:2>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list