我不知道为什么您的查询返回的ID比需要的多。这个$args
对于get\\u,用户看起来是正确的。
默认情况下,get\\u用户不支持orderby=rand
, 但您可以覆盖该选项。见下文:
function random_user_query( &$query )
{
$query->query_orderby = "ORDER BY RAND()";
}
// Usage: [random_users how_many = 3]
add_shortcode( \'random_users\', \'display_random_users\' );
function display_random_users( $atts ) {
shortcode_atts( array( "how_many" => \'\', ), $atts );
// Create a function to override the ORDER BY clause
add_action( \'pre_user_query\', \'random_user_query\' );
$users = get_users( array(
\'role\' => \'subscriber\',
\'fields\' => \'ID\',
\'orderby\' => \'rand\',
\'number\' => $atts[\'how_many\'],
) );
foreach ( $users as $userID ) {
printf(
\'<a href="%s">%s<br />%s</a><br />\',
bp_core_get_user_domain( $userID ),
bp_core_fetch_avatar( array( \'item_id\' => $userID ) ),
xprofile_get_field_data( \'1\', $userID )
);
}
// Remove the hook
remove_action( \'pre_user_query\', \'random_user_query\' );
}
这样,您只需查询$how\\u many用户,而不是查询所有用户,然后过滤掉$how\\u many。
我找到了这个钩子here.