A list of users with a specific role as a link.
-
I want to make a list of a specific role. I get to the list itself, but then I do not get the link. I want each name to link to their profile page.
I have tried all sorts of different variants and has now tried and tested me out for several weeks. Here are some different tests.
-
You are getting links in some cases. Pick the most promising variant and focus on debugging that until it works consistently for all users. There’s either a problem with your code’s logic or the underlying data to yield inconsistent results.
Post the code you want to work with here (or at pastebin.com if it’s extensive) if you want some help with it.
Thanks, here is the most promising code.
<p>7</p> <?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> <a href="' . $author_post_url . '" title="' . sprintf( __( "Posts by %s" ), $curuser->user_nicename ) . '" ' . '>' . $curuser->display_name .'</a></p>'; } } } ?>
<p>10</p> <?php // WP_User_Query arguments $args = array ( 'role' => 'fotograf', 'order' => 'ASC', 'orderby' => 'display_name', 'search' => '*'.esc_attr( $search_term ).'*', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'first_name', 'value' => $search_term, 'compare' => 'LIKE' ), array( 'key' => 'last_name', 'value' => $search_term, 'compare' => 'LIKE' ), array( 'key' => 'description', 'value' => $search_term , 'compare' => 'LIKE' ) ) ); // Create the WP_User_Query object $wp_user_query = new WP_User_Query($args); // Get the results $authors = $wp_user_query->get_results(); // Check for results if (!empty($authors)) { echo '<ul>'; // loop trough each author foreach ($authors as $author) { // get all the user's data $fotografer = get_userdata($author->ID); echo '<li>'.$fotografer->first_name.' '.$fotografer->last_name.'</li>'; } echo '</ul>'; } else { echo 'Inga fotografer registrerade med dina s?ktermer'; } ?>
<p>11</p> <?php // WP_User_Query arguments $args = array ( 'role' => 'fotograf', 'order' => 'ASC', 'orderby' => 'display_name', 'search' => '*'.esc_attr( $search_term ).'*', 'meta_query' => array( 'relation' => 'OR', array( 'key' => 'first_name', 'value' => $search_term, 'compare' => 'LIKE' ), array( 'key' => 'last_name', 'value' => $search_term, 'compare' => 'LIKE' ), array( 'key' => 'description', 'value' => $search_term , 'compare' => 'LIKE' ) ) ); // Create the WP_User_Query object $wp_user_query = new WP_User_Query($args); // Get the results $authors = $wp_user_query->get_results(); // Check for results if (!empty($authors)) { echo '<p> '. $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> <a href="' . $author_post_url . '" title="' . sprintf( __( "Posts by %s" ), $curuser->user_nicename ) . '" ' . '> ' . $curuser->display_name .' </a></p>'; } } } ?>
It needs a little work, but I like the second approach the best. It avoids low level techniques like using internal globals and direct SQL queries. Nothing wrong with low level, I just prefer to avoid it when possible.
This version merely needs the name output to be made into links. Like as done in the other examples, use
get_author_posts_url()
to get the links. This is assuming that fotograf roles have posts credited to them or that your theme’s author template displays bios even without posts credited to them.If that assumption is wrong, then you need to modify the author template.
Thanks bcworkz.
I’m not sure I understand what you mean.
I have created my own theme, so I have no problem to change it.
But do not understand what I should change?Still testing, but nothing works.
echo ‘<p> 1 </p>’;
echo ‘ID).'”> ‘ . $fotografer->user_firstname . ‘ ‘ . $fotografer->user_lastname . ‘ ‘;echo ‘<p> 2 </p>’;
echo ‘ID).'”> ‘ . $fotografer->user_firstname . ‘ ‘ . $fotografer->user_lastname . ‘ ‘;echo ‘<p> 3 </p>’;
echo ‘ ‘ . $fotografer->user_firstname . ‘ ‘ . $fotografer->user_lastname . ‘ ‘;echo ‘<p> 4 </p>’;
echo ‘ ‘ . $fotografer->user_firstname . ‘ ‘ . $fotografer->user_lastname . ‘ ‘;Great news!
I’m sorry I missed your post last week, it was not my intention to ignore you.
No problem. ??
Thanks anyway, nice that I found a solution.
- The topic ‘A list of users with a specific role as a link.’ is closed to new replies.