Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>
                    </div>
                <?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
add comment

1 Answer

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
1  
@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
add comment

Your Answer

 
discard

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.