GeneratorsQueryUser Query Builder

User Query Builder

Query users by role, capability, meta and search — with the role vs role__in mixup and the count_total cost called out before you ship it.

user-query.php
Saved just now
Who
2 roles · role__in
Wrap in * for a wildcard. Without one it is an exact match.
Meta clauses
none
No meta clauses. User meta lives in one big table — filtering on it is a JOIN per clause, same as posts.
Shape of the result
count_total
Runs a second COUNT query so get_total() has a number.
has_published_posts
Only users who have actually published something.
The class, with get_results() and the total when you need pagination.
// Generated with WP CodeKit — powered by GrowQuest (https://growquest.io).
$args = array(
	'role__in'            => array( 'author', 'editor' ),
	'number'              => 20,
	'orderby'             => 'display_name',
	'has_published_posts' => true,
);

$query = new WP_User_Query( $args );

$total = $query->get_total();

if ( ! empty( $query->get_results() ) ) {
	echo '<ul>';

	foreach ( $query->get_results() as $user ) {
		printf(
			'<li><a href="%1$s">%2$s</a></li>',
			esc_url( get_author_posts_url( $user->ID ) ),
			esc_html( $user->display_name )
		);
	}

	echo '</ul>';

	printf(
		'<p>%s</p>',
		esc_html( sprintf( _n( '%s user', '%s users', $total, 'mytheme' ), number_format_i18n( $total ) ) )
	);
} else {
	esc_html_e( 'No users matched.', 'mytheme' );
}