Make WordPress Core

Opened 12 years ago

Closed 11 years ago

Last modified 11 years ago

#20606 closed feature request (wontfix)

orderby rand param does not work with either WP_User_Query or get_users

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

Description

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)

#1 @kovshenin
12 years ago

  • Cc kovshenin@… added

#2 @kurtpayne
12 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.

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

#3 @c3mdigital
11 years ago

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

order by clause is filterable. Closing as wontfix

#4 @markoheijnen
11 years 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.

#5 @helen
11 years ago

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