Skip to content
wp-skills
Register
tools/wordpress-user-query-generator

WordPress User Query Generator

Build a WP_User_Query call: filter by role, meta or a wildcard search, choose what the query returns, and get a loop that matches it — without paying for a total count you never read.

'role' requires every listed role; 'role__in' requires any one of them.

Comma separated role slugs.

Note the capitalisation: WP_User_Query takes 'ID', not 'ids'. The generated loop adapts to your choice.

Wrapped in wildcards for you — WP_User_Query matches the whole column without them.

Comma separated. Leave blank to let WordPress choose.

Maximum users to return. Leave blank for all of them.

Adds SQL_CALC_FOUND_ROWS. Off is faster when you only need the page you are showing.

snippet.php
$args = array(
	'role'        => 'author',
	'orderby'     => 'display_name',
	'order'       => 'ASC',
	'number'      => 10,
	'count_total' => false,
);

$user_query = new WP_User_Query( $args );

// get_results() rather than a ->results property: WP_User_Query exposes the
// matches through this method, and it is what returns the shape 'fields' asked
// for.
$users = $user_query->get_results();

if ( ! empty( $users ) ) {
	echo '<ul>';

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

	echo '</ul>';
} else {
	printf( '<p>%s</p>', esc_html__( 'No users found.', 'text-domain' ) );
}