Make WordPress Core

Opened 3 years ago

Closed 20 months ago

Last modified 20 months ago

#20606 closed feature request (wontfix)

orderby rand param does not work with either WP_User_Query or get_users

Reported by: kjmeath Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.2.1
Component: Users Keywords: close
Focuses: Cc:


Using WordPress 3.2.1

$contributors = get_users(array('role' => 'contributor', 'orderby' => 'rand', 'number' => 3));
$wp_user_search = new WP_User_Query(array('role' => 'contributor', 'orderby' => 'rand', 'number' => 3));
$contributors = $wp_user_search->get_results();	

Change History (5)

comment:1 @kovshenin3 years ago

  • Cc kovshenin@… added

comment:2 @kurtpayne3 years ago

  • Cc kpayne@… added

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.


// 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 );

comment:3 @c3mdigital20 months ago

  • Keywords close added
  • Resolution set to wontfix
  • Status changed from new to closed

order by clause is filterable. Closing as wontfix

comment:4 @markoheijnen20 months ago

This can been seen as a feature request for the API. So closing because we have a filter for it doesn't have to fit.

comment:5 @helen20 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.