2

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

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' ) )
2
  • 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, 2013 at 18:22
  • 1
    @talven I can't imagine this to cause any issues, it's pretty contained and informational function :)
    – Rarst
    Jun 18, 2013 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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