WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a bunch of custom user taxonomies which I use to group users by, and then show these users on the front end with the following code (from taxonomy-{term}.php)

<?php // Get each user from taxonomy
            $term_id = get_queried_object_id(); // Get Term ID
            $term = get_queried_object(); // Get term
            $users = get_objects_in_term( $term_id, $term->taxonomy );
            if ( !empty( $users ) && (/* Condition based on role here */) ) {
                <?php foreach ( $users as $user_id ) { ?>
                    <div class="user-card">
                        <?php /* Image */ echo get_avatar( get_the_author_meta( 'user_email', $user_id ), '204' ); ?>
                        <h2><?php /* Name */ the_author_meta( 'first_name', $user_id ); ?> <?php the_author_meta( 'last_name', $user_id ); ?></h2>
                        <p><?php /* Short Bio */ the_author_meta( 'short_bio', $user_id ); ?></p>
                <?php } ?>
            <?php } ?>

The code is working just fine, but I would like to refine the results based on what role the users have. I have a role called "invisible" which lets the user access their profile but they won't be visible on the site until i change the role to one that is visible. Ideally I would like to use something like if (!user_has_role('invisible'))

I've tried different solutions but I just can't seem to get it right.

share|improve this question
up vote 1 down vote accepted

There is more generic version of current_user_can() - user_can() which takes user and capability or role to check for.

So your pseudo code would become something like:

if ( ! user_can( $user_id, 'invisible' ) )
share|improve this answer
I modified the foreach to this which worked perfectly. I shouldn't expect any issues? <?php foreach ( $users as $user_id ) if (!user_can( $user_id, 'osynlig' )) { ?> – talven Jun 18 '13 at 18:22
@talven I can't imagine this to cause any issues, it's pretty contained and informational function :) – Rarst Jun 18 '13 at 18:25
Excellent, thanks :) – talven Jun 18 '13 at 19:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.