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.

1 Answer
1

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' ) )

Leave a Reply

Your email address will not be published. Required fields are marked *