WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] display info of user with certain role or level ini WP 2.7 (19 posts)

  1. Jim de Groot
    Member
    Posted 5 years ago #

    Hi,

    i've searching on the forum for a way to check a user role or level, but i haven't managed to find a solution for my problem yet.
    I want to have page with an overview of the blog's authors, but I want to seperate them based on their role or level. So at the top of the page the authors with admin role, next comes the editors, and last the regular authors/bloggers.

    I've been trying to do it with if($curauth->user_level == x) but that wasn't working. I think the user_level isn't available in wp2.7.

    Can anyone please help me? Thanks!

  2. scribu
    Member
    Posted 5 years ago #

    Use wp_list_authors()

  3. Jim de Groot
    Member
    Posted 5 years ago #

    How can I seperate on different roles with wp_list_authors()?
    Does it have an argument for roles or levels?

  4. MichaelH
    Member
    Posted 5 years ago #

    Try this:

    <?php
    //list each role and each user with that role
    global $wp_roles;
    foreach( $wp_roles->role_names as $role => $name ) {
      $name = translate_with_context($name);
      echo '<p>List of users in the role '.$role .' ('. $name . '):</p>';
      $this_role = "'[[:<:]]".$role."[[:>:]]'";
      $query = "SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000";
      $users_of_this_role = $wpdb->get_results($query);
      if ($users_of_this_role) {
        foreach($users_of_this_role as $user) {
          $curuser = get_userdata($user->ID);
          $author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
          echo '<p>--User nicename: '.$curuser->user_nicename .', display Name: '. $curuser->display_name . ', link to author posts <a href="' . $author_post_url . '" title="' . sprintf( __( "Posts by %s" ), $curuser->user_nicename ) . '" ' . '>' . $curuser->user_nicename .'</a></p>';
        }
      }
    }
    ?>
  5. Jim de Groot
    Member
    Posted 5 years ago #

    Thanks Michael, with your help I could make it work the way I wanted.

  6. Jim de Groot
    Member
    Posted 5 years ago #

    Yesterday I thought I had the right solution, but now I discovered a new problem. It works in Firefox but not in Internet Explorer.
    In IE there is this php error:
    "Warning: Invalid argument supplied for foreach()" for the line:
    foreach( $wp_roles->role_names as $role => $name )

    Can you help me again MichaelH?

  7. scribu
    Member
    Posted 5 years ago #

    That't not because you're using IE, but because you're not logged in ->

    $wp_roles is empty. I know because I've tried this before and I had to resort to hardcoding the roles:

    $roles = array('admin', 'editor', etc.);

    foreach( $roles as $role ) ...

  8. Jim de Groot
    Member
    Posted 5 years ago #

    Thanks for your quick reaction scribu.

    I'm new to wp and php, so i'm not familiar with the functions and variables inside the code example.
    I'm not sure how to apply your suggestion to my code, because I want to test on the roles in their Dutch translated names like this:

    <?php
    
    /* Loop through users to search for the admin and editor users. */
    //list each role and each user with that role
    global $wp_roles;
    foreach( $wp_roles->role_names as $role => $name )
    {
      	$name = translate_with_context($name);
      	if($name == 'Beheerder' || $name == 'Bewerker')
      	{
    	  	//echo '+Users in the role '.$role .' ('. $name . '):';
    	  	$this_role = "'[[:<:]]".$role."[[:>:]]'";
    	  	$query = "SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000";
    	  	$users_of_this_role = $wpdb->get_results($query);
    	  	if ($users_of_this_role)
    		{
    	    	foreach($users_of_this_role as $user)
    			{
    		    	$curuser = get_userdata($user->ID);
    		      	$author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
    				echo "<div class='post'>";
    					echo "<a href='.$user_link.' title='.$curuser->display_name.'>";
    						echo "<h2>$curuser->display_name</h2>";
    					echo '</a>';
    					echo "<a href='.$user_link.' title='.$curuser->display_name'>";
    						echo get_avatar($curuser->user_email, '80', $avatar);
    					echo '</a>';
    					echo ''.$curuser->description.'';
    				echo '</div>';
    		    }
    	  	}
    	}
    }
    ?>

    How should I use this array to get the same functionality?
    $roles = array('Beheerder', 'Bewerker', 'Auteur', 'Abonnee');
    "Beheerder" is an admin
    "Bewerker" is an editor
    "Auteur" is an author
    "Abonnee" is a subscriber

  9. Jim de Groot
    Member
    Posted 5 years ago #

    I've done it now with this code:

    $roles = array('administrator', 'editor', 'author', 'contributor', 'subscriber');
    
    /* Loop through users to search for the admin and editor users. */
    foreach( $roles as $role )
    {
      	// all users with admin or editor role
    	if($role == 'administrator' || $role == 'editor')
      	{
    	  	$this_role = "'[[:<:]]".$role."[[:>:]]'";
    	  	$query = "SELECT * FROM $wpdb->users WHERE ID = ANY (SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value RLIKE $this_role) ORDER BY user_nicename ASC LIMIT 10000";
    	  	$users_of_this_role = $wpdb->get_results($query);
    	  	if ($users_of_this_role)
    		{
    	    	foreach($users_of_this_role as $user)
    			{
    		    	$curuser = get_userdata($user->ID);
    		      	$author_post_url=get_author_posts_url($curuser->ID, $curuser->nicename);
    				echo "<div class='post'>";
    					echo "<a href='.$user_link.' title='.$curuser->display_name.'>";
    						echo "<h2>$curuser->display_name</h2>";
    					echo '</a>';
    					echo "<a href='.$user_link.' title='.$curuser->display_name'>";
    						echo get_avatar($curuser->user_email, '80', $avatar);
    					echo '</a>';
    					echo '<p>'.$curuser->description.'</p>';
    				echo '</div>';
    		    }
    	  	}
    	}
    }
    ?>

    I stoppped matching on $name, but used $role instead. That way I won't have to use the translation.

  10. scribu
    Member
    Posted 5 years ago #

    Glad to see you figured it out.

  11. Vast HTML
    Member
    Posted 5 years ago #

    is there a way to make that code so it shows in pages? i have over 300 member and would rather show like 25 or 50 to a page..

  12. Jim de Groot
    Member
    Posted 5 years ago #

    Maybe you can add some extra constraints to decrease the number of members to show. For example test on the number of submitted posts or something like that.

    For paging you could look how its done for archive pages. Maybe you could figure out how it is done there with wp_pagenavi. I'm not sure if it is build in for archive pages or if it is part of the WP-PageNavi plugin I am using.

  13. Vast HTML
    Member
    Posted 5 years ago #

    In order to save space i made it so it would only show the members avatar, now just to find a way to may a tooltip show on the avatar and ill be ok haha, you can view here

    http://jericosystems.com/members/

  14. zhaval
    Member
    Posted 4 years ago #

    Hi, this is exactly what I need but when I tried scribu's script this is what I get (no users listed)

    List of users in the role administrator (Administrator):
    List of users in the role editor (Editor):
    List of users in the role author (Author):
    List of users in the role contributor (Contributor):
    List of users in the role subscriber (Subscriber):

    and Jim de Groot's code produces an empty page. What am I doing wrong? thanks for your answer in advance!

  15. scribu
    Member
    Posted 4 years ago #

    Have you tried MichaelH's version?

    Turn on WP_DEBUG in wp-config.php and see if you get any errors on that page.

  16. zhaval
    Member
    Posted 4 years ago #

    Yes, sorry, when I wrote "scribu's" script I actually thought of MichelH's script. It produces an OK list of user roles as shown above, but no users are displayed, although there are users in some categories, obviously. WP_DEBUG did not give any clues.. thanks, scribu!

  17. tjmapes
    Member
    Posted 4 years ago #

    @Jim de Groot

    your code there (so far) helped me more than you ever know. With the combination of the USER COMMUNITY plugin where I can create my own roles, I can now use this to separate out the roles on different pages (ie members / artists)...

    Amazing!! Thank you so much

  18. livelearncreate
    Member
    Posted 4 years ago #

    Fantastic Post!

  19. shagdirty
    Member
    Posted 4 years ago #

    I know this is an old post but...

    @Jim de Groot's last code block is perfect for what I'm trying to do but I'd also like to display any posts by the filtered users as well. So, if a user has a specific custom role assigned, display his or her last X number of posts. I've done some digging but to no avail. I'm not great with php so any help would be appreciated.

    thanks, Shag

Topic Closed

This topic has been closed to new replies.

About this Topic