WordPress как на ладони
Недорогой хостинг для сайтов на WordPress: wordpress.jino.ru Платформа для конвертации и монетизации трафика
функция не описана

acf_get_grouped_users() ACF 5.0.0

This function will return all users grouped by role This is handy for select settings

Хуков нет.

Возвращает

(Массив).

Использование

acf_get_grouped_users( $args );
$args **
-
По умолчанию: array()

Список изменений

С версии 5.0.0 Введена.

Код acf_get_grouped_users() ACF 5.9.1

<?php
function acf_get_grouped_users( $args = array() ) {
	
	// vars
	$r = array();
	
	
	// defaults
	$args = wp_parse_args( $args, array(
		'users_per_page'			=> -1,
		'paged'						=> 0,
		'role'         				=> '',
		'orderby'					=> 'login',
		'order'						=> 'ASC',
	));
	
	
	// offset
	$i = 0;
	$min = 0;
	$max = 0;
	$users_per_page = acf_extract_var($args, 'users_per_page');
	$paged = acf_extract_var($args, 'paged');
	
	if( $users_per_page > 0 ) {
		
		// prevent paged from being -1
		$paged = max(0, $paged);
		
		
		// set min / max
		$min = (($paged-1) * $users_per_page) + 1; // 	1, 	11
		$max = ($paged * $users_per_page); // 			10,	20
		
	}
	
	
	// find array of post_type
	$user_roles = acf_get_pretty_user_roles($args['role']);
	
	
	// fix role
	if( is_array($args['role']) ) {
		
		// global
   		global $wp_version, $wpdb;
   		
   		
		// vars
		$roles = acf_extract_var($args, 'role');
		
		
		// new WP has role__in
		if( version_compare($wp_version, '4.4', '>=' ) ) {
			
			$args['role__in'] = $roles;
				
		// old WP doesn't have role__in
		} else {
			
			// vars
			$blog_id = get_current_blog_id();
			$meta_query = array( 'relation' => 'OR' );
			
			
			// loop
			foreach( $roles as $role ) {
				
				$meta_query[] = array(
					'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
					'value'   => '"' . $role . '"',
					'compare' => 'LIKE',
				);
				
			}
			
			
			// append
			$args['meta_query'] = $meta_query;
			
		}
		
	}
	
	
	// get posts
	$users = get_users( $args );
	
	
	// loop
	foreach( $user_roles as $user_role_name => $user_role_label ) {
		
		// vars
		$this_users = array();
		$this_group = array();
		
		
		// populate $this_posts
		foreach( array_keys($users) as $key ) {
			
			// bail ealry if not correct role
			if( !in_array($user_role_name, $users[ $key ]->roles) ) continue;
		
			
			// extract user
			$user = acf_extract_var( $users, $key );
			
			
			// increase
			$i++;
			
			
			// bail ealry if too low
			if( $min && $i < $min ) continue;
			
			
			// bail early if too high (don't bother looking at any more users)
			if( $max && $i > $max ) break;
			
			
			// group by post type
			$this_users[ $user->ID ] = $user;
			
			
		}
		
		
		// bail early if no posts for this post type
		if( empty($this_users) ) continue;
		
		
		// append
		$r[ $user_role_label ] = $this_users;
					
	}
	
	
	// return
	return $r;
	
}